DIYgod/RSSHub · error

vm.Script.runInContext is not supported in Workers

Error message

vm.Script.runInContext is not supported in Workers

What it means

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.

Source

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

import * as timers from 'node:timers';
import * as timers_promises from 'node:timers/promises';
import * as tls from 'node:tls';
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: () => {

View on GitHub (pinned to bed535e087)

Solutions

  1. Disable script execution in JSDOM (omit runScripts or set 'outside-only').
  2. Replace vm-based eval with a Workers-safe alternative (no code execution, or precompiled logic).
  3. Gate the code path to the Node runtime only.
  4. Drop the dependency for Worker-deployed routes.

Example fix

// before
const dom = new JSDOM(html, { runScripts: 'dangerously' });
// after
const dom = new JSDOM(html, { runScripts: 'outside-only' });
Defensive patterns

Strategy: validation

Validate before calling

const vmUsable = typeof process !== 'undefined' && !!process.versions?.node;
if (!vmUsable) {
  // skip vm.Script(...).runInContext; disable JSDOM runScripts
}

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).runInContext(ctx);
  else return staticResult;
} catch (e) {
  if (isVmShimError(e)) return staticResult;
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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