{"record":{"id":"e8acb954584a6ce8","repo":"denoland/deno","slug":"err-vm-module-different-context","errorCode":"ERR_VM_MODULE_DIFFERENT_CONTEXT","errorMessage":"Linked modules must use the same context","messagePattern":"Linked modules must use the same context","errorType":"error_code","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/vm.js","lineNumber":683,"sourceCode":"    const linkerPromises = [];\n    for (let i = 0; i < requests.length; i++) {\n      const { specifier, attributes } = requests[i];\n      ArrayPrototypePush(specifiers, specifier);\n      const p = PromiseResolve(\n        linker(specifier, this, { attributes, assert: attributes }),\n      );\n      ArrayPrototypePush(linkerPromises, p);\n    }\n    const resolvedModules = await SafePromiseAll(linkerPromises);\n\n    const wraps = [];\n    for (let i = 0; i < resolvedModules.length; i++) {\n      const m = resolvedModules[i];\n      if (!isModule(m)) {\n        throw new ERR_VM_MODULE_NOT_MODULE();\n      }\n      if (m.context !== this[kContext]) {\n        throw new ERR_VM_MODULE_DIFFERENT_CONTEXT();\n      }\n      ArrayPrototypePush(wraps, m[kWrap]);\n    }\n\n    // Record this module's resolutions before recursing so a dependency that\n    // imports back into this module finds it already in `visited`.\n    op_vm_module_link(this[kWrap], specifiers, wraps);\n\n    for (let i = 0; i < resolvedModules.length; i++) {\n      await resolvedModules[i][kLinkGraph](linker, visited);\n    }\n  }\n\n  evaluate(options = { __proto__: null }) {\n    try {\n      validateObject(options, \"options\");\n      const status = op_vm_module_get_status(this[kWrap]);\n      // Allow evaluate from linked (2), evaluating (3), evaluated (4), errored (5).","sourceCodeStart":665,"sourceCodeEnd":701,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/vm.js#L665-L701","documentation":"Thrown by vm.Module#link() when the linker returns a module that was created against a different context than the module being linked. Every module records the `context` from its options object in kContext; link() enforces `m.context === this[kContext]` (vm.js:682) because a V8 module graph must be instantiated within a single context.","triggerScenarios":"Creating the root module with `new vm.SourceTextModule(code)` (no context) but returning dependencies built with `new vm.SourceTextModule(code, { context: someContext })`, or vice versa; using two different context objects from two vm.createContext() calls for root and dependencies; sharing a cached module created for context A while linking a graph rooted in context B.","commonSituations":"A linker that caches modules in a Map keyed only by specifier, ignoring which context they were created for, then reuses that cache for a second run under a fresh vm.createContext(); tutorials that create the dependency inside `vm.createContext(...)` while the root module is context-less; refactoring code so only some modules receive options.context.","solutions":["Pass the exact same context value to every module in the graph: derive it inside the linker from `mod.context` (the second link() argument)","If the root module has no context, omit `context` in dependency module options too (undefined must match undefined)","Key any linker-side module cache by `${specifier}\\0${context ?? 'default'}` so modules are never reused across contexts","Re-create cached dependency modules when the incoming module's context differs from the cached one"],"exampleFix":"// before\nconst ctx = vm.createContext({});\nconst root = new vm.SourceTextModule(code); // no context\nroot.link(async (spec) => new vm.SourceTextModule(files[spec], { context: ctx })); // ERR_VM_MODULE_DIFFERENT_CONTEXT\n// after\nconst ctx = vm.createContext({});\nconst root = new vm.SourceTextModule(code, { context: ctx });\nroot.link(async (spec, mod) =>\n  new vm.SourceTextModule(files[spec], { context: mod.context }));","handlingStrategy":"type-guard","validationCode":"const deps = await Promise.all(requests.map((r) => linker(r.specifier, mod)));\nfor (const d of deps) {\n  if (d.context !== mod.context) throw new Error(`context mismatch for ${d.identifier}`);\n}","typeGuard":"const sameContext = (a, b) => a.context === b.context; // undefined must match undefined","tryCatchPattern":"mod.link(linker).catch((e) => {\n  if (e.code === 'ERR_VM_MODULE_DIFFERENT_CONTEXT') {\n    // rebuild dependency modules with mod.context, then retry with a FRESH root module\n  } else throw e;\n});","preventionTips":["Create the context once per sandbox and thread it to every module constructor","Inside linkers, read the requesting module's context via the second callback argument","Key linker caches by (specifier, context) pairs"],"tags":["vm","context","module-linking","node-compat"],"backgroundTag":"vm-module-context-mismatch","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}