DIYgod/RSSHub · error

vm.Script.runInNewContext is not supported in Workers

Error message

vm.Script.runInNewContext is not supported in Workers

What it means

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.

Source

Thrown at lib/shims/node-module.ts:55

import * as tty from 'node:tty';
import * as url from 'node:url';
import * as util from 'node:util';
import * as util_types from 'node:util/types';
import * as worker_threads from 'node:worker_threads';
import * as zlib from 'node:zlib';

// VM shim for Cloudflare Workers
// JSDOM and some other libraries require vm module
class ScriptShim {
    private code: string;
    constructor(code: string) {
        this.code = code;
    }
    runInContext() {
        throw new Error('vm.Script.runInContext is not supported in Workers');
    }
    runInNewContext() {
        throw new Error('vm.Script.runInNewContext is not supported in Workers');
    }
    runInThisContext() {
        throw new Error('vm.Script.runInThisContext is not supported in Workers');
    }
}

const vmShim = {
    createContext: (sandbox?: object) => sandbox || {},
    runInContext: () => {
        throw new Error('vm.runInContext is not supported in Workers');
    },
    runInNewContext: () => {
        throw new Error('vm.runInNewContext is not supported in Workers');
    },
    runInThisContext: () => {
        throw new Error('vm.runInThisContext is not supported in Workers');
    },
    Script: ScriptShim,

View on GitHub (pinned to bed535e087)

Solutions

  1. Avoid runInNewContext in Worker code; precompile or skip the eval path.
  2. Branch on runtime and only use vm on Node.
  3. Remove the dependency that requires sandboxed execution.

Example fix

// before
new vm.Script(code).runInNewContext(sandbox);
// after — precompile and ship static logic, or run this path on Node only
export const result = precompiledHandler(input);
Defensive patterns

Strategy: validation

Validate before calling

const vmUsable = typeof process !== 'undefined' && !!process.versions?.node;
if (!vmUsable) {
  // do not call runInNewContext; use a non-eval path
}

Type guard

function isVmShimError(e: unknown): e is Error {
  return e instanceof Error && /not supported in Workers/.test(e.message);
}

Try / catch

try {
  if (vmUsable) new vm.Script(code).runInNewContext(sandbox);
  else return precompiled(input);
} catch (e) {
  if (isVmShimError(e)) return precompiled(input);
  throw e;
}

Prevention

When it happens

Trigger: Calling new vm.Script(code).runInNewContext(sandbox) under Workers — typical of sandboxing libraries and some polyfills.

Common situations: A dependency that isolates execution per sandbox; JSDOM script execution; dynamic code generation tooling.

Related errors


AI-assisted analysis of DIYgod/RSSHub@bed535e087 (2026-08-12). Data as JSON: /api/errors/0d7d80f2e66b5ac1. Report an issue: GitHub.