{"record":{"id":"3b89e380c2cfdc35","repo":"DIYgod/RSSHub","slug":"require-is-not-available-in-workers-attempted-t","errorCode":null,"errorMessage":"require() is not available in Workers. Attempted to require: ${id}","messagePattern":"require\\(\\) is not available in Workers\\. Attempted to require: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/shims/node-module.ts","lineNumber":194,"sourceCode":"    'fs/promises': fs_promises,\n    'node:stream/promises': stream_promises,\n    'stream/promises': stream_promises,\n    'node:stream/web': stream_web,\n    'stream/web': stream_web,\n    'node:util/types': util_types,\n    'util/types': util_types,\n    'node:timers/promises': timers_promises,\n    'timers/promises': timers_promises,\n    'node:vm': vmShim,\n};\n\nexport function createRequire(_filename: string | URL) {\n    return function require(id: string): unknown {\n        if (Object.hasOwn(builtinModules, id)) {\n            return builtinModules[id];\n        }\n        // For non-builtin modules, throw an error\n        throw new Error(`require() is not available in Workers. Attempted to require: ${id}`);\n    };\n}\n\nexport default {\n    createRequire,\n};\n","sourceCodeStart":176,"sourceCodeEnd":201,"githubUrl":"https://github.com/DIYgod/RSSHub/blob/bed535e0879dc71c5aff6f1e7bd1ac21ede40115/lib/shims/node-module.ts#L176-L201","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Convert the consumer to an ESM import so the bundler resolves the package at build time.","Bundle the needed package (mark it non-external) so Workers receives it statically.","Avoid createRequire-based plugin loading under Workers.","Run the affected route on the Node target where real require() is available."],"exampleFix":"// before\nconst { createRequire } = require('module');\nconst req = createRequire(__filename);\nconst lib = req('some-cjs-pkg');\n// after\nimport someCjsPkg from 'some-cjs-pkg'; // resolved by the bundler at build time","handlingStrategy":"validation","validationCode":"// before calling createRequire().require(id), check the allowlist\nimport { builtinModules } from 'module'; // Node-only reference; under Workers the shim holds the map\nconst ALLOWED = new Set(Object.keys(builtinModules));\nfunction canRequire(id: string): boolean {\n  return ALLOWED.has(id);\n}\nif (!canRequire(id)) {\n  // do not require(); use a static ESM import instead\n}","typeGuard":"function isWorkersRequireError(e: unknown): e is Error {\n  return e instanceof Error && /require\\(\\) is not available in Workers/.test(e.message);\n}","tryCatchPattern":"try {\n  return createRequireFn(id);\n} catch (e) {\n  if (isWorkersRequireError(e)) {\n    // fall back to an ESM-imported module resolved at build time\n    return bundledModule;\n  }\n  throw e;\n}","preventionTips":["Prefer ESM imports over createRequire for third-party packages under Workers.","Bundle (non-externalize) any package a CJS dep needs so the bundler resolves it.","Never use require() for plugin loading of npm packages under Workers."],"tags":["cloudflare-workers","require","cjs","bundler","shim"],"backgroundTag":null,"analyzedSha":"bed535e0879dc71c5aff6f1e7bd1ac21ede40115","analyzedAt":"2026-08-12T19:29:35.364Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}