agents icon indicating copy to clipboard operation
agents copied to clipboard

CodeMode fails in local development with "Uncaught ReferenceError: __filename is not defined"

Open GurRonenExplorium opened this issue 2 months ago • 2 comments

Describe the bug When trying to use codemode feature in local development with wrangler dev, the Worker fails to start with a fatal error: Uncaught ReferenceError: __filename is not defined. This occurs because the json-schema-to-typescript library (a runtime dependency of codemode) requires Node.js filesystem APIs that are not available in the Cloudflare Workers runtime, even with the nodejs_compat, experimental, enable_ctx_exports], compatibility flags enabled.

To Reproduce Add codemode to an existing mcp-auth0-oidc repo (or likely any other repo) and try to run wrangler dev,

Expected behavior The codemode feature should initialize successfully and wrap the provided tools for code generation workflows. The Worker should start without errors in local development.

Version:

  • Agents SDK: 0.2.20 (latest stable)
  • Wrangler: 4.45.3

Additional context This was completed while following instructions in the official codemode docs https://github.com/cloudflare/agents/blob/main/docs/codemode.md + referencing the example there of codemode as well

GurRonenExplorium avatar Nov 02 '25 16:11 GurRonenExplorium

For now you can workaround this with https://github.com/cloudflare/agents/blob/3668b9c780993db1ca78cf164a87189e4a694db1/examples/codemode/vite.config.ts#L8

threepointone avatar Nov 03 '25 09:11 threepointone

Thanks for the workaround! for posterity and anyone else trying to get it to work a workaround was to put in my wrangler.jsonc the following changes

  "define": {
    "__filename": "'index.ts'",
    "__dirname": "'/'"
  }

since we aren't using vite, it's a pure server side worker, no vite.

GurRonenExplorium avatar Nov 03 '25 14:11 GurRonenExplorium