ERR_VM_MODULE_CANNOT_CREATE_CACHED_DATA
Node.jsERRORCriticalModuleHIGH confidence

Cannot create cached data for an already-evaluated vm module

Production Risk

Low — affects performance optimisation paths, not core functionality.

What this means

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.

Why it happens
  1. 1Calling createCachedData() after mod.evaluate() has been called
  2. 2Attempting to cache a module that was created with existing cached data and then evaluated
How to reproduce

Triggered when code-cache generation is attempted on a module that has already been evaluated.

trigger — this will error
trigger — this will error
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

expected 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

Generate cached data before evaluation
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.

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

Call createCachedData() after evaluate()

V8 cannot produce a pristine bytecode cache after the module has mutated its internal state during execution.

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