-
Notifications
You must be signed in to change notification settings - Fork 447
Description
Problem
When installing @cloudflare/codemode from npm (v0.1.2) and deploying to Cloudflare Workers, the deploy fails with:
Uncaught ReferenceError: __filename is not defined
at typescript.js:8487 in isFileSystemCaseSensitive
...
at zod-to-ts/dist/index.js:1:34
Root cause
@cloudflare/codemode depends on zod-to-ts@^2.0.0, which has typescript as a peer dependency. The zod-to-ts import is in types-CpkEgXwN.js, which is eagerly imported by both ai.js and index.js:
// dist/ai.js
import { n as sanitizeToolName, t as generateTypes } from "./types-CpkEgXwN.js";
// dist/types-CpkEgXwN.js
import { createAuxiliaryTypeStore, createTypeAlias, printNode, zodToTs } from "zod-to-ts";typescript is a CJS package that uses __filename at the top level, which doesn't exist in the Workers runtime. This causes the deploy validation to fail with error code 10021.
Workaround
Using a file: link to the local monorepo package works because the monorepo build/bundling handles tree-shaking differently. The published npm package does not.
Suggested fix
Either:
- Lazy-import
zod-to-tsso it's only loaded whengenerateTypes()is actually called (not at module load time) - Split the chunks so that
ai.js(which providescreateCodeTool) doesn't import the types module unlessgenerateTypesis explicitly used - Replace
zod-to-tswith a lighter schema-to-TS converter that doesn't depend on the fulltypescriptpackage
Option 1 is probably the smallest change — a dynamic import("zod-to-ts") inside generateTypes() would prevent typescript from being eagerly loaded.