ERR_VM_MODULE_STATUS
Node.jsERRORCriticalModuleHIGH confidence

vm.Module operation invalid for current module status

Production Risk

Low — vm module APIs are experimental/advanced; incorrect usage is caught immediately at runtime.

What this means

Thrown when a vm.Module method is called but the modules current lifecycle status does not permit that operation. For example, evaluate() requires the module to be in "linked" status; calling it on an "unlinked" or "errored" module raises this error.

Why it happens
  1. 1Calling evaluate() before link() has completed
  2. 2Calling link() on a module that is already in "evaluating" or "evaluated" status
  3. 3Calling setExport() on a SyntheticModule outside the evaluate callback
How to reproduce

Triggered when a vm.Module lifecycle method is invoked out of sequence.

trigger — this will error
trigger — this will error
import vm from 'node:vm';
const mod = new vm.SourceTextModule('export const x = 1;');
// Forgot to call mod.link() first
await mod.evaluate(); // throws ERR_VM_MODULE_STATUS

expected output

Error [ERR_VM_MODULE_STATUS]: Module status must be one of linked, evaluated, or errored to call evaluate()

Fix

Follow the correct vm.Module lifecycle order

WHEN Always — link before evaluating

Follow the correct vm.Module lifecycle order
import vm from 'node:vm';
const mod = new vm.SourceTextModule('export const x = 1;');
await mod.link(async () => {}); // link first
await mod.evaluate();           // then evaluate
console.log(mod.namespace.x);  // 1

Why this works

Respecting the lifecycle order (unlinked → linked → evaluated) satisfies the internal status checks.

Code examples
Triggerjs
import vm from 'node:vm';
const mod = new vm.SourceTextModule('export const x = 1;');
// Forgot to call mod.link() first
await mod.evaluate(); // throws ERR_VM_MODULE_STATUS  // this triggers ERR_VM_MODULE_STATUS
Handle in try/catchjs
try {
  // operation that may throw ERR_VM_MODULE_STATUS
  riskyOperation()
} catch (err) {
  if (err.code === 'ERR_VM_MODULE_STATUS') {
    console.error('ERR_VM_MODULE_STATUS:', err.message)
  } else {
    throw err
  }
}
Defensive pattern to avoid itjs
// Validate inputs before calling the operation
function safe_err_vm_module_status(...args) {
  // validate args here
  return performOperation(...args)
}
What not to do

Skip the link() step before evaluate()

The module graph must be fully resolved before execution can begin.

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