drizzle-team / drizzle-team/drizzle-orm
[BUG]: ReferenceError: entityKind is not defined in production builds (sideEffects: false + scope hoisting)
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### Report hasn't been filed before.
- [x] I have verified that the bug I'm about to report hasn't been filed before.
### What version of `drizzle-orm` are you using?
1.0.0-beta.15-859cf75
### What version of `drizzle-kit` are you using?
1.0.0-beta.15-859cf75
### Other packages
_No response_
### Describe the Bug
The following was mostly created by claude. I did verify that the temporary solution (patching `drizzle-orm` to use `static [Symbol.for("...")] = ...` instead of `static [entityKind] = ....` resolves the problem). I didn't see that during development but when I deployed to production it immediately broke my app.
Maybe there is a way to make it work with rspack without the patch. If so and someone knows about it, it'd be great if you could share and maybe document that on the website. 🙏
## Description
### What is the undesired behavior?
Production builds crash at runtime with `ReferenceError: entityKind is not defined`. The error comes from class static field definitions like `static [entityKind] =
"ManyV2"` in `relations.js`, `enum.js`, `sequence.js`, etc.
The root cause is twofold:
1. `package.json` declares `"sideEffects": false`, which tells bundlers they can tree-shake any module whose exports aren't directly used. But `entity.js` exports
`entityKind = Symbol.for("drizzle:entityKind")` which is consumed as a computed property key (`static [entityKind] = "..."`) in every class across the package. When
bundlers drop `entity.js`, `entityKind` becomes undefined.
2. Even after patching `sideEffects` to `true`, rspack's scope hoisting (module concatenation) inconsistently rewrites the imported `entityKind` binding. In the bundled
output of `relations.js`, `OneV2` gets correctly rewritten to a module reference (`_entity_js__rspack_import_0.i`), but `ManyV2` — in the same file, using identical syntax
— is left as a bare `entityKind` variable. When that class ends up in a chunk where `entityKind` was never defined, it throws `ReferenceError`.
This does not occur in development mode because tree-shaking and scope hoisting are disabled.
### What are the steps to reproduce it?
1. Use `drizzle-orm@1.0.0-beta.15` in a project bundled with rspack (Rsbuild) or webpack
2. Import and use `defineRelations`, table definitions, or column types (e.g. `pgEnum`, `pgTable`)
3. Build with `NODE_ENV=production`
4. Open the app in a browser
ReferenceError: entityKind is not defined
at enum.js:6
at sequence.js:25
at relations.js:134
### What is the desired result?
Production builds should work without runtime errors. The `entityKind` Symbol should be available in all modules that reference it, regardless of how the bundler
concatenates or splits chunks.
**Suggested fix**: Replace all `static [entityKind] = "..."` with `static [Symbol.for("drizzle:entityKind")] = "..."` across the codebase. `Symbol.for()` is idempotent and
returns the same Symbol globally regardless of module evaluation order, making it immune to tree-shaking and scope hoisting. Additionally, remove or correct the
`"sideEffects": false` declaration in `package.json`.
### Additional context
- **Database engine**: PostgreSQL (via PGlite in-browser and Neon serverless), but the issue affects all drivers since `entityKind` is in the shared core
- **Driver**: `drizzle-orm/pglite`, `drizzle-orm/effect-postgres`, but not driver-specific — the bug is in the core module structure
- **Monorepo**: Yes, pnpm workspaces (pnpm 10.x). Internal packages depend on `drizzle-orm` via `workspace:*` and are aliased to their TypeScript source in the bundler
config. drizzle-orm is resolved through pnpm's symlinked `node_modules`.
- **Bundler**: rspack via Rsbuild, production mode. Any bundler that respects `sideEffects: false` and performs scope hoisting (webpack, rspack) would be affected.
Vite/esbuild/wrangler are likely unaffected since they handle module concatenation differently.
- **Runtime**: Browser (SPA deployed to Cloudflare Pages)
- **drizzle-orm version**: `1.0.0-beta.15-859cf75`
### Workaround
pnpm `patchedDependencies` patch that:
1. Sets `"sideEffects": true` in `package.json`
2. Replaces all ~500 `static [entityKind]` with `static [Symbol.for("drizzle:entityKind")]` in all `.js` files
Contributor guide
Assessment
This issue has not been assessed yet.