ERR_VM_MODULE_LINK_FAILURE
Node.jsERRORCriticalModuleHIGH confidence

vm.Module linking failed due to an error in the linker

Production Risk

Low — confined to vm sandbox usage; the module becomes permanently unusable after this error.

What this means

Thrown when the linker function supplied to vm.Module.link() throws or returns a rejected Promise. The error wraps the original linker error and indicates that the module graph could not be fully resolved, leaving the module in an errored state.

Why it happens
  1. 1The linker function throws synchronously when resolving a specifier
  2. 2The linker returns a Promise that rejects (e.g. file not found)
  3. 3An unhandled exception inside an async linker
How to reproduce

Triggered when any invocation of the linker callback fails during vm.Module.link().

trigger — this will error
trigger — this will error
import vm from 'node:vm';
const mod = new vm.SourceTextModule('import x from "missing"');
await mod.link(async (spec) => {
  throw new Error(`Cannot find ${spec}`);
});

expected output

Error [ERR_VM_MODULE_LINK_FAILURE]: Link failure
Caused by: Error: Cannot find missing

Fix

Handle all specifiers in the linker and throw only for truly unresolvable ones

WHEN When building a custom module resolver

Handle all specifiers in the linker and throw only for truly unresolvable ones
const modules = new Map([['dep', depModule]]);
await mod.link(async (specifier) => {
  const m = modules.get(specifier);
  if (!m) throw new Error(`Unknown specifier: ${specifier}`);
  return m;
});

Why this works

Ensuring the linker can resolve all specifiers prevents the link-failure wrapper error.

Code examples
Triggerjs
import vm from 'node:vm';
const mod = new vm.SourceTextModule('import x from "missing"');
await mod.link(async (spec) => {
  throw new Error(`Cannot find ${spec}`);
});  // this triggers ERR_VM_MODULE_LINK_FAILURE
Handle in try/catchjs
try {
  // operation that may throw ERR_VM_MODULE_LINK_FAILURE
  riskyOperation()
} catch (err) {
  if (err.code === 'ERR_VM_MODULE_LINK_FAILURE') {
    console.error('ERR_VM_MODULE_LINK_FAILURE:', err.message)
  } else {
    throw err
  }
}
Defensive pattern to avoid itjs
// Validate inputs before calling the operation
function safe_err_vm_module_link_failure(...args) {
  // validate args here
  return performOperation(...args)
}
What not to do

Let the linker throw for expected dependencies

Any linker throw causes the module to enter a permanent errored state and cannot be retried.

Same error in other languages
Sources
Official documentation ↗

Node.js Error Codes Documentation

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Node.js errors