{"record":{"id":"16ce346471cc7d57","repo":"mastra-ai/mastra","slug":"require-specifier-is-not-available-in-cloudfla","errorCode":null,"errorMessage":"require(${specifier}) is not available in Cloudflare Workers","messagePattern":"require\\((.+?)\\) is not available in Cloudflare Workers","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"deployers/cloudflare/src/index.ts","lineNumber":175,"sourceCode":"    // Write readable-stream stub — redirects to native node:stream available via nodejs_compat.\n    // readable-stream is a userland copy of Node.js streams used by packages like elevenlabs.\n    // Bundling it for Workers pulls in Node.js polyfills (abort-controller, process/, string_decoder/)\n    // that are unnecessary and fail to resolve. The native node:stream is API-compatible.\n    const readableStreamStubPath = 'readable-stream-stub.mjs';\n    const readableStreamStub = `// Redirect readable-stream to native node:stream (available via nodejs_compat)\nimport stream from 'node:stream';\nexport const { Readable, Writable, Duplex, Transform, PassThrough, Stream, pipeline, finished } = stream;\nexport default stream;\n`;\n    await writeFile(join(outputDirectory, this.outputDir, readableStreamStubPath), readableStreamStub);\n\n    // Write module stub — Wrangler runs Workers with an undefined import.meta.url,\n    // so eager createRequire(import.meta.url) interop helpers must not call Node's implementation.\n    const moduleStubPath = 'module-stub.mjs';\n    const moduleStub = `// Stub for module.createRequire in Cloudflare Workers\nexport function createRequire() {\n  const req = specifier => {\n    throw new Error(\\`require(\\${specifier}) is not available in Cloudflare Workers\\`);\n  };\n  req.resolve = specifier => specifier;\n  return req;\n}\nexport default { createRequire };\n`;\n    await writeFile(join(outputDirectory, this.outputDir, moduleStubPath), moduleStub);\n\n    const wranglerConfig: Unstable_RawConfig = {\n      name: 'mastra',\n      compatibility_date: '2025-04-01',\n      compatibility_flags: ['nodejs_compat', 'nodejs_compat_populate_process_env'],\n      observability: {\n        logs: {\n          enabled: true,\n        },\n      },\n      ...userConfig,","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/deployers/cloudflare/src/index.ts#L157-L193","documentation":"This error is thrown by a module stub that the Cloudflare deployer writes during `writeFiles` (called via `prepare`) into the deploy bundle. Wrangler runs Workers with an undefined `import.meta.url`, so Node's `createRequire(import.meta.url)` interop helper cannot work; the stub replaces `module.createRequire` with a function that throws this explicit message whenever bundled code calls `require(...)`. It exists to turn a cryptic runtime crash into a clear diagnostic: Node's `require` is unavailable in the Cloudflare Workers runtime.","triggerScenarios":"Deployed Worker code (or a bundled dependency) eagerly calls `require('...')` or `const require = createRequire(import.meta.url); require(...)` at module evaluation time, hitting the module-stub.mjs stub inside the Workers runtime.","commonSituations":"Importing an npm package with CJS interop shims that detect `import.meta.url`; publishing CJS-only dependencies into a Worker bundle; running Node-written code on Workers; bundler resolving the CJS entry instead of the ESM build.","solutions":["Find the `require(...)` / `createRequire(import.meta.url)` call in the stack trace and replace it with a static ESM `import`.","Configure your bundler to resolve the ESM/browser build of the offending dependency (mainFields/alias/`conditions: ['worker','browser']`).","Mark the module external and provide a Workers-compatible shim, or enable `nodejs_compat` compat flags if only limited Node APIs are needed.","Run `wrangler dev` before deploying to catch the stub throw locally."],"exampleFix":"// before\nimport { createRequire } from 'module';\nconst require = createRequire(import.meta.url);\nconst pkg = require('./package.json');\n// after\nimport pkg from './package.json' with { type: 'json' };","handlingStrategy":"fallback","validationCode":"// Detect Node require/createRequire usage before deploying to Workers\nimport { readFileSync } from 'node:fs';\nconst src = readFileSync(entryFile, 'utf8');\nif (/createRequire\\s*\\(/.test(src) || /(^|[^.\\w])require\\s*\\(\\s*['\"]/.test(src)) {\n  throw new Error('Entry uses require()/createRequire() which is unavailable in Cloudflare Workers; migrate to ESM imports.');\n}","typeGuard":"function isNodeInteropUsage(code: string): boolean {\n  return /createRequire|\\brequire\\s*\\(/.test(code);\n}","tryCatchPattern":"try {\n  await deployer.prepare(...);\n} catch (err) {\n  if (err instanceof Error && err.message.includes('is not available in Cloudflare Workers')) {\n    console.error('CJS interop detected: replace require()/createRequire with ESM imports or add nodejs_compat.', err.message);\n  } else throw err;\n}","preventionTips":["Audit entry code and dependencies for require/createRequire usage before each deploy.","Prefer ESM-only dependencies (module/browser exports) in Worker projects.","Test the bundle with `wrangler dev`, which reproduces the undefined import.meta.url environment.","Treat the module-stub error message as a pointer to CJS interop code in your bundle."],"tags":["cloudflare","workers","esm","require","runtime"],"backgroundTag":"require-not-available-in-workers","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}