vm.Module operation invalid for current module status
Production Risk
Low — vm module APIs are experimental/advanced; incorrect usage is caught immediately at runtime.
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.
- 1Calling evaluate() before link() has completed
- 2Calling link() on a module that is already in "evaluating" or "evaluated" status
- 3Calling setExport() on a SyntheticModule outside the evaluate callback
Triggered when a vm.Module lifecycle method is invoked out of sequence.
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_STATUSexpected 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
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); // 1Why this works
Respecting the lifecycle order (unlinked → linked → evaluated) satisfies the internal status checks.
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_STATUStry {
// 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
}
}// Validate inputs before calling the operation
function safe_err_vm_module_status(...args) {
// validate args here
return performOperation(...args)
}✕ Skip the link() step before evaluate()
The module graph must be fully resolved before execution can begin.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev