{"record":{"id":"6aa2244013f8b687","repo":"DIYgod/RSSHub","slug":"vm-script-runincontext-is-not-supported-in-workers","errorCode":null,"errorMessage":"vm.Script.runInContext is not supported in Workers","messagePattern":"vm\\.Script\\.runInContext is not supported in Workers","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/shims/node-module.ts","lineNumber":52,"sourceCode":"import * as timers from 'node:timers';\nimport * as timers_promises from 'node:timers/promises';\nimport * as tls from 'node:tls';\nimport * as tty from 'node:tty';\nimport * as url from 'node:url';\nimport * as util from 'node:util';\nimport * as util_types from 'node:util/types';\nimport * as worker_threads from 'node:worker_threads';\nimport * as zlib from 'node:zlib';\n\n// VM shim for Cloudflare Workers\n// JSDOM and some other libraries require vm module\nclass ScriptShim {\n    private code: string;\n    constructor(code: string) {\n        this.code = code;\n    }\n    runInContext() {\n        throw new Error('vm.Script.runInContext is not supported in Workers');\n    }\n    runInNewContext() {\n        throw new Error('vm.Script.runInNewContext is not supported in Workers');\n    }\n    runInThisContext() {\n        throw new Error('vm.Script.runInThisContext is not supported in Workers');\n    }\n}\n\nconst vmShim = {\n    createContext: (sandbox?: object) => sandbox || {},\n    runInContext: () => {\n        throw new Error('vm.runInContext is not supported in Workers');\n    },\n    runInNewContext: () => {\n        throw new Error('vm.runInNewContext is not supported in Workers');\n    },\n    runInThisContext: () => {","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/DIYgod/RSSHub/blob/bed535e0879dc71c5aff6f1e7bd1ac21ede40115/lib/shims/node-module.ts#L34-L70","documentation":"Guard inside the ScriptShim class of lib/shims/node-module.ts. The Workers runtime exposes no vm/V8-context API, so executing compiled script code is impossible; runInContext() throws. The shim exists so modules that merely import vm (JSDOM etc.) load without error — only actual execution fails, at the call site.","triggerScenarios":"A dependency that runs JS at runtime via new vm.Script(code).runInContext(...) under Workers — e.g. JSDOM with runScripts enabled, vm2, or a template engine that compiles.","commonSituations":"Using a JSDOM-based route with script execution turned on; an SSR/eval library; copying Node example code that uses vm into a Worker route.","solutions":["Disable script execution in JSDOM (omit runScripts or set 'outside-only').","Replace vm-based eval with a Workers-safe alternative (no code execution, or precompiled logic).","Gate the code path to the Node runtime only.","Drop the dependency for Worker-deployed routes."],"exampleFix":"// before\nconst dom = new JSDOM(html, { runScripts: 'dangerously' });\n// after\nconst dom = new JSDOM(html, { runScripts: 'outside-only' });","handlingStrategy":"validation","validationCode":"const vmUsable = typeof process !== 'undefined' && !!process.versions?.node;\nif (!vmUsable) {\n  // skip vm.Script(...).runInContext; disable JSDOM runScripts\n}","typeGuard":"function isVmShimError(e: unknown): e is Error {\n  return e instanceof Error && /not supported in Workers/.test(e.message);\n}","tryCatchPattern":"try {\n  if (vmUsable) new vm.Script(code).runInContext(ctx);\n  else return staticResult;\n} catch (e) {\n  if (isVmShimError(e)) return staticResult;\n  throw e;\n}","preventionTips":["Do not enable JSDOM script execution under Workers.","Replace vm-based eval with precompiled/static logic.","Keep vm execution on the Node runtime."],"tags":["cloudflare-workers","vm","runtime","shim","jsdom"],"backgroundTag":null,"analyzedSha":"bed535e0879dc71c5aff6f1e7bd1ac21ede40115","analyzedAt":"2026-08-12T19:29:35.364Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}