DIYgod/RSSHub · error

vm.runInContext is not supported in Workers

Error message

vm.runInContext is not supported in Workers

What it means

Top-level vmShim.runInContext() in lib/shims/node-module.ts throws. Unlike createContext() (which returns the sandbox unchanged) and isContext() (returns false), the execution functions are hard-fail because Workers cannot run vm code. The shim mirrors Node's vm module shape but only the non-executing members are usable.

Source

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

    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,
    isContext: () => false,
    compileFunction: () => {
        throw new Error('vm.compileFunction is not supported in Workers');
    },
};

// Child process shim (inline to avoid import cycle)
const child_process = {
    execSync: (_command: string): Buffer => Buffer.from(''),
    exec: () => {

View on GitHub (pinned to bed535e087)

Solutions

  1. Eliminate vm.runInContext usage from Worker-deployed code.
  2. Switch to a non-eval implementation (string templating, precompiled handlers).
  3. Reserve this code path for the Node runtime.

Example fix

// before
vm.runInContext(compiledCode, sandbox);
// after
const out = renderTemplate(tplString, data); // pure-string, no eval
Defensive patterns

Strategy: validation

Validate before calling

const vmUsable = typeof process !== 'undefined' && !!process.versions?.node;
if (!vmUsable) {
  // skip vm.runInContext; use string templating
}

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) vm.runInContext(code, ctx);
  else return render(template, data);
} catch (e) {
  if (isVmShimError(e)) return render(template, data);
  throw e;
}

Prevention

When it happens

Trigger: Calling vm.runInContext(code, sandbox) directly (not via new Script) under Workers — common in older sandboxing/eval utilities.

Common situations: JSDOM-style or vm2-style callers using the functional vm API; template renderers that compile+run in one step.

Related errors


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