DIYgod/RSSHub · error

vm.Script.runInThisContext is not supported in Workers

Error message

vm.Script.runInThisContext is not supported in Workers

What it means

ScriptShim.runInThisContext() in lib/shims/node-module.ts throws — Workers forbid executing compiled code in the current context too. The whole Script execution surface is intentionally non-functional.

Source

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

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

View on GitHub (pinned to bed535e087)

Solutions

  1. Remove the runInThisContext call from Worker code paths.
  2. Use static imports / bundler-resolved modules instead of runtime compilation.
  3. Run the affected logic on the Node target.

Example fix

// before
new vm.Script(code).runInThisContext();
// after
import handler from './handler'; // resolved at build time
Defensive patterns

Strategy: validation

Validate before calling

const vmUsable = typeof process !== 'undefined' && !!process.versions?.node;
if (!vmUsable) {
  // do not call runInThisContext; use static imports
}

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

Prevention

When it happens

Trigger: Calling new vm.Script(code).runInThisContext() under Workers; some legacy require()-hooks and vm-based loaders use this entry point.

Common situations: Module loaders that compile wrappers; old code paths migrated from Node without runtime audit.

Related errors


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