{"record":{"id":"0d7d80f2e66b5ac1","repo":"DIYgod/RSSHub","slug":"vm-script-runinnewcontext-is-not-supported-in-work","errorCode":null,"errorMessage":"vm.Script.runInNewContext is not supported in Workers","messagePattern":"vm\\.Script\\.runInNewContext is not supported in Workers","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/shims/node-module.ts","lineNumber":55,"sourceCode":"import * 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: () => {\n        throw new Error('vm.runInThisContext is not supported in Workers');\n    },\n    Script: ScriptShim,","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/DIYgod/RSSHub/blob/bed535e0879dc71c5aff6f1e7bd1ac21ede40115/lib/shims/node-module.ts#L37-L73","documentation":"ScriptShim.runInNewContext() in lib/shims/node-module.ts throws because Workers cannot create a fresh V8 context and execute code in it. Identical rationale to runInContext: import-safe, execution-unsafe by design.","triggerScenarios":"Calling new vm.Script(code).runInNewContext(sandbox) under Workers — typical of sandboxing libraries and some polyfills.","commonSituations":"A dependency that isolates execution per sandbox; JSDOM script execution; dynamic code generation tooling.","solutions":["Avoid runInNewContext in Worker code; precompile or skip the eval path.","Branch on runtime and only use vm on Node.","Remove the dependency that requires sandboxed execution."],"exampleFix":"// before\nnew vm.Script(code).runInNewContext(sandbox);\n// after — precompile and ship static logic, or run this path on Node only\nexport const result = precompiledHandler(input);","handlingStrategy":"validation","validationCode":"const vmUsable = typeof process !== 'undefined' && !!process.versions?.node;\nif (!vmUsable) {\n  // do not call runInNewContext; use a non-eval path\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).runInNewContext(sandbox);\n  else return precompiled(input);\n} catch (e) {\n  if (isVmShimError(e)) return precompiled(input);\n  throw e;\n}","preventionTips":["Avoid sandboxed code execution under Workers.","Precompile snippets at build time.","Gate vm execution to Node."],"tags":["cloudflare-workers","vm","runtime","shim"],"backgroundTag":null,"analyzedSha":"bed535e0879dc71c5aff6f1e7bd1ac21ede40115","analyzedAt":"2026-08-12T19:29:35.364Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}