Value passed to vm linker is not a vm.Module instance
Production Risk
Low — vm module usage is typically confined to sandboxing/testing infrastructure.
Thrown when the linker function passed to vm.Module.link() returns a value that is not a vm.Module instance. The linker is responsible for resolving imported specifiers and must return a proper vm.Module (e.g. vm.SourceTextModule) for each dependency.
- 1Linker function returns a plain object instead of a vm.Module
- 2Linker returns undefined or a Promise that resolves to a non-Module
- 3Accidentally returning a native module instead of wrapping it in vm.SyntheticModule
Triggered when vm.Module.link() processes the return value of the user-supplied linker function.
import vm from 'node:vm';
const mod = new vm.SourceTextModule('import x from "dep"');
await mod.link(() => ({ not: 'a module' })); // wrong return typeexpected output
Error [ERR_VM_MODULE_NOT_MODULE]: Provided module is not an instance of Module
Fix
Return a vm.SyntheticModule or vm.SourceTextModule from the linker
WHEN Always — the linker must return a proper vm.Module
import vm from 'node:vm';
const dep = new vm.SyntheticModule(['default'], function() {
this.setExport('default', 42);
});
const mod = new vm.SourceTextModule('import x from "dep"');
await mod.link(async (specifier) => dep);
await mod.evaluate();Why this works
Returning a vm.Module subclass satisfies the type check performed by the link() internals.
import vm from 'node:vm';
const mod = new vm.SourceTextModule('import x from "dep"');
await mod.link(() => ({ not: 'a module' })); // wrong return type // this triggers ERR_VM_MODULE_NOT_MODULEtry {
// operation that may throw ERR_VM_MODULE_NOT_MODULE
riskyOperation()
} catch (err) {
if (err.code === 'ERR_VM_MODULE_NOT_MODULE') {
console.error('ERR_VM_MODULE_NOT_MODULE:', err.message)
} else {
throw err
}
}// Validate inputs before calling the operation
function safe_err_vm_module_not_module(...args) {
// validate args here
return performOperation(...args)
}✕ Return plain objects from the linker
The VM linker requires a vm.Module instance to set up the module graph correctly.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev