ESM support in Prisma Client
I'm facing difficulty spinning up my backend application when using a shared package that contains the Prisma DB client. For context, my server uses the ESM module type, and I was wondering if it's possible to convert the generated Prisma client into ESM.
I explored Prisma’s moduleFormat: "esm" option, but it seems to omit key exports like error classes and other members from the Prisma namespace. Can you help me understand what's going wrong or if there's an additional fix required?
I'm using Turborepo and tsup to compile my database package and also generate .d.ts files to export types. Let me know if anything specific needs to be configured to make ESM work properly.
2 Replies
You've gone full bot mode! Let me fetch that AI answer for you ASAP.
Prisma’s ESM support is still evolving and has some important caveats, especially when using the new
You’re not missing anything—ESM support is still incomplete, and the missing exports are a known limitation. You can follow the progress and provide feedback on the official issue ESM Support Issue. For now, consider the above workarounds or stick to CJS if you need the full Prisma namespace and error classes.
moduleFormat = "esm" option in the generator block. This option does generate an ESM-compatible Prisma Client, but as you noticed, it currently omits some exports—such as error classes and the full Prisma namespace—which can break type imports or error handling in ESM projects. This is a known limitation of the early-access ESM generator as of Prisma v6.6.0+ New ESM Generator Announcement.
Key points and workarounds:
- The ESM generator is still experimental and does not yet export all members (like error classes or the full Prisma namespace). This is being tracked and improved, but there’s no complete solution yet.
- If you need the missing exports, you may need to stick with the CommonJS client for now, or use workarounds such as re-exporting types and enums manually from your generated client or using a hybrid import pattern ESM Workarounds.
- For type safety, you can import types directly from @prisma/client (these are always ESM-compatible), but runtime imports of the client itself may require using createRequire or similar hacks if you’re not using the ESM generator Type-safe ESM workaround.
- When using tools like Turborepo and tsup, ensure your build outputs preserve ESM format and do not accidentally convert the Prisma client back to CJS. Also, avoid double-compiling the generated client.
Summary:You’re not missing anything—ESM support is still incomplete, and the missing exports are a known limitation. You can follow the progress and provide feedback on the official issue ESM Support Issue. For now, consider the above workarounds or stick to CJS if you need the full Prisma namespace and error classes.
Was the bot answer helpful?
I responded to your query here:
https://discord.com/channels/937751382725886062/1387167657832218734/1387450281934979082