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.
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.
- 1The linker function throws synchronously when resolving a specifier
- 2The linker returns a Promise that rejects (e.g. file not found)
- 3An unhandled exception inside an async linker
Triggered when any invocation of the linker callback fails during vm.Module.link().
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
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.
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_FAILUREtry {
// 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
}
}// Validate inputs before calling the operation
function safe_err_vm_module_link_failure(...args) {
// validate args here
return performOperation(...args)
}✕ Let the linker throw for expected dependencies
Any linker throw causes the module to enter a permanent errored state and cannot be retried.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev