Cannot create cached data for an already-evaluated vm module
Production Risk
Low — affects performance optimisation paths, not core functionality.
Thrown when vm.Script.createCachedData() or a related API is used on a vm.SourceTextModule that has already been evaluated. Cached data (V8 code cache) can only be generated before the module has executed, because evaluation mutates internal V8 state.
- 1Calling createCachedData() after mod.evaluate() has been called
- 2Attempting to cache a module that was created with existing cached data and then evaluated
Triggered when code-cache generation is attempted on a module that has already been evaluated.
import vm from 'node:vm';
const mod = new vm.SourceTextModule('export const x = 1;');
await mod.link(() => {});
await mod.evaluate();
mod.createCachedData(); // throws — too lateexpected output
Error [ERR_VM_MODULE_CANNOT_CREATE_CACHED_DATA]: Cannot create cached data for a module which has been evaluated
Fix
Generate cached data before evaluation
WHEN When you want to cache compiled module bytecode
import vm from 'node:vm';
const mod = new vm.SourceTextModule('export const x = 1;');
await mod.link(() => {});
const cache = mod.createCachedData(); // before evaluate()
await mod.evaluate();Why this works
The V8 code cache is produced from the compiled (but not yet executed) module; calling it pre-evaluation captures that state.
import vm from 'node:vm';
const mod = new vm.SourceTextModule('export const x = 1;');
await mod.link(() => {});
await mod.evaluate();
mod.createCachedData(); // throws — too late // this triggers ERR_VM_MODULE_CANNOT_CREATE_CACHED_DATAtry {
// operation that may throw ERR_VM_MODULE_CANNOT_CREATE_CACHED_DATA
riskyOperation()
} catch (err) {
if (err.code === 'ERR_VM_MODULE_CANNOT_CREATE_CACHED_DATA') {
console.error('ERR_VM_MODULE_CANNOT_CREATE_CACHED_DATA:', err.message)
} else {
throw err
}
}// Validate inputs before calling the operation
function safe_err_vm_module_cannot_create_cached_data(...args) {
// validate args here
return performOperation(...args)
}✕ Call createCachedData() after evaluate()
V8 cannot produce a pristine bytecode cache after the module has mutated its internal state during execution.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev