ERR_VM_MODULE_NOT_MODULE
Node.jsERRORCriticalModuleHIGH confidence

Value passed to vm linker is not a vm.Module instance

Production Risk

Low — vm module usage is typically confined to sandboxing/testing infrastructure.

What this means

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.

Why it happens
  1. 1Linker function returns a plain object instead of a vm.Module
  2. 2Linker returns undefined or a Promise that resolves to a non-Module
  3. 3Accidentally returning a native module instead of wrapping it in vm.SyntheticModule
How to reproduce

Triggered when vm.Module.link() processes the return value of the user-supplied linker function.

trigger — this will error
trigger — this will error
import vm from 'node:vm';
const mod = new vm.SourceTextModule('import x from "dep"');
await mod.link(() => ({ not: 'a module' })); // wrong return type

expected 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

Return a vm.SyntheticModule or vm.SourceTextModule from the linker
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.

Code examples
Triggerjs
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_MODULE
Handle in try/catchjs
try {
  // 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
  }
}
Defensive pattern to avoid itjs
// Validate inputs before calling the operation
function safe_err_vm_module_not_module(...args) {
  // validate args here
  return performOperation(...args)
}
What not to do

Return plain objects from the linker

The VM linker requires a vm.Module instance to set up the module graph correctly.

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