DIYgod/RSSHub · error

require() is not available in Workers. Attempted to require:

Error message

require() is not available in Workers. Attempted to require: ${id}

What it means

createRequire in lib/shims/node-module.ts implements require() against a fixed allowlist of Node builtins (fs, path, vm, stream, etc.). Workers cannot synchronously load arbitrary npm packages, so any require('<non-builtin>') throws, embedding the offending id in the message. ESM imports still work normally — only CJS require of third-party packages fails.

Source

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

    'fs/promises': fs_promises,
    'node:stream/promises': stream_promises,
    'stream/promises': stream_promises,
    'node:stream/web': stream_web,
    'stream/web': stream_web,
    'node:util/types': util_types,
    'util/types': util_types,
    'node:timers/promises': timers_promises,
    'timers/promises': timers_promises,
    'node:vm': vmShim,
};

export function createRequire(_filename: string | URL) {
    return function require(id: string): unknown {
        if (Object.hasOwn(builtinModules, id)) {
            return builtinModules[id];
        }
        // For non-builtin modules, throw an error
        throw new Error(`require() is not available in Workers. Attempted to require: ${id}`);
    };
}

export default {
    createRequire,
};

View on GitHub (pinned to bed535e087)

Solutions

  1. Convert the consumer to an ESM import so the bundler resolves the package at build time.
  2. Bundle the needed package (mark it non-external) so Workers receives it statically.
  3. Avoid createRequire-based plugin loading under Workers.
  4. Run the affected route on the Node target where real require() is available.

Example fix

// before
const { createRequire } = require('module');
const req = createRequire(__filename);
const lib = req('some-cjs-pkg');
// after
import someCjsPkg from 'some-cjs-pkg'; // resolved by the bundler at build time
Defensive patterns

Strategy: validation

Validate before calling

// before calling createRequire().require(id), check the allowlist
import { builtinModules } from 'module'; // Node-only reference; under Workers the shim holds the map
const ALLOWED = new Set(Object.keys(builtinModules));
function canRequire(id: string): boolean {
  return ALLOWED.has(id);
}
if (!canRequire(id)) {
  // do not require(); use a static ESM import instead
}

Type guard

function isWorkersRequireError(e: unknown): e is Error {
  return e instanceof Error && /require\(\) is not available in Workers/.test(e.message);
}

Try / catch

try {
  return createRequireFn(id);
} catch (e) {
  if (isWorkersRequireError(e)) {
    // fall back to an ESM-imported module resolved at build time
    return bundledModule;
  }
  throw e;
}

Prevention

When it happens

Trigger: A dependency using module.createRequire(filename) to load a CJS third-party (non-builtin) module at runtime under Workers. The id is anything not present in builtinModules (e.g. a package name or relative path).

Common situations: JSDOM or similar libs calling require() on a sub-package; a CJS shim probing for an optional dependency via require(); code ported from Node that uses createRequire for plugin loading.

Related errors


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