DIYgod/RSSHub · error

vm.runInNewContext is not supported in Workers

Error message

vm.runInNewContext is not supported in Workers

What it means

vmShim.runInNewContext() in lib/shims/node-module.ts throws — Workers cannot spin up a new isolated context for code execution. This is the functional-form counterpart to ScriptShim.runInNewContext and fails for the same runtime reason.

Solutions

  1. Do not call runInNewContext under Workers.
  2. Precompile the snippet at build time or replace with static logic.
  3. Move the eval-dependent route to Node.

Example fix

// before
vm.runInNewContext(code, sandbox);
// after
const fn = precompiledFns[name]; // build-time map, no runtime eval
Defensive patterns

Strategy: validation

Validate before calling

const vmUsable = typeof process !== 'undefined' && !!process.versions?.node;
if (!vmUsable) {
  // skip vm.runInNewContext; precompile instead
}

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.runInNewContext(code, sandbox);
  else return precompiledFns[name](data);
} catch (e) {
  if (isVmShimError(e)) return precompiledFns[name](data);
  throw e;
}

Prevention

When it happens

Trigger: Calling vm.runInNewContext(code, sandbox) under Workers; some bundlers/loaders and sandbox libraries use this form.

Common situations: Sandboxing third-party/untrusted code; polyfills that evaluate snippets per-call.

Related errors


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

Appendix: source

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

    }
    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: () => {
        throw new Error('exec is not supported in Cloudflare Workers');
    },
    spawn: () => {

View on GitHub (pinned to bed535e087)