vite: Unable to bundle Prisma for serverless functions
Describe the bug
I started with a default vercel template for react router v7, deployed it on vercel, everything works. Now when i add prisma and start the build, vite says ".prisma/client/default" is imported by ".prisma/client/default?commonjs-external", but could not be resolved – treating it as an external dependency. and deployment crashes with an internal server error.
I had already raised this issue on react-router but they closed it saying that this isn’t a react router bug
Reproduction
https://github.com/dhruvkaushik305/testing-prisma.git
Steps to reproduce
run npm run build and the cli shows that error message
System Info
System:
OS: Linux 6.1 Debian GNU/Linux 12 (bookworm) 12 (bookworm)
CPU: (8) x64 Intel(R) Core(TM) i5-10210U CPU @ 1.60GHz
Memory: 14.32 GB / 19.20 GB
Container: Yes
Shell: 5.9 - /usr/bin/zsh
Binaries:
Node: 22.12.0 - ~/.nvm/versions/node/v22.12.0/bin/node
npm: 10.9.0 - ~/.nvm/versions/node/v22.12.0/bin/npm
pnpm: 9.14.4 - ~/.nvm/versions/node/v22.12.0/bin/pnpm
bun: 1.1.38 - ~/.bun/bin/bun
Browsers:
Brave Browser: 131.1.73.101
Used Package Manager
npm
Logs
These are the server logs on vercel
TypeError [ERR_INVALID_MODULE_SPECIFIER]: Invalid module ".prisma/client/default" is not a valid package name imported from /var/task/assets/server-build-Bi4c-HXU.js
at parsePackageName (node:internal/modules/esm/resolve:786:11)
at packageResolve (node:internal/modules/esm/resolve:809:5)
at moduleResolve (node:internal/modules/esm/resolve:931:18)
at moduleResolveWithNodePath (node:internal/modules/esm/resolve:1173:14)
at defaultResolve (node:internal/modules/esm/resolve:1216:79)
at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:542:12)
at ModuleLoader.resolve (node:internal/modules/esm/loader:510:25)
at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:239:38)
at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:96:40)
at link (node:internal/modules/esm/module_job:95:36)
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 24 (15 by maintainers)
I’ll probably replace it with drizzle, in case I still continue on this I’ll start this as a discussion as you said. Thank you for your time and efforts
More simple reproduction: https://stackblitz.com/edit/github-jxnpwg49?file=server%2Fapp.ts,vite.config.ts,package.json&terminal=build (needs to run locally as prisma does not work in stackblitz)
It seems prisma generates
node_modules/.prisma/clientand imports that fromnode_modules/@prisma/client/default.jsbyrequire('.prisma/client/default'). Whilerequire('.prisma/client/default')works, Vite does not support that because it’s not supported for imports (i.e.import '.prisma/client/default'errors withERR_INVALID_MODULE_SPECIFIER(spec). Technically, Vite should allow that to be resolved forrequire, but I think it’s better to be avoided and the default generated location should be changed on prisma side.For a workaround, you can change the location. https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/generating-prisma-client#the-location-of-prisma-client For example, update
schema.prismato include the following code:and update the import to use
@prisma/client-generated(instead of@prisma/client).It does, thanks, I think that’s the same way OP wants to use Prisma. Sorry for misjudging.
@silvenon Thanks for the insights. I was facing issues mentioned here - https://github.com/prisma/prisma/issues/26153. Particularly when building and deploying the app built with react router in Vercel.
Though this issue https://github.com/remix-run/react-router/issues/12610 got closed, there are still open issues on the same topic like https://github.com/prisma/prisma/issues/26153.
With this solution I mentioned above, I was able to deploy the app in Vercel with Prisma Client that works on serverless environment. I hope this clarifies the intent.
If the fix is not too complicated, I think Vite would accept a PR. But probably won’t be working on a fix as a team since it’s kind a deprecated feature. In that case, I think we can have a section in https://vite.dev/guide/troubleshooting that tells it is not supported.
Regarding Vite not supporting
__dirnameand__filenamewhen no-externalized, I think Vite can replace them todirname(fileURLToPath(import.meta.url))andfileURLToPath(import.meta.url)sinceimport.meta.dirname/import.meta.filenamealready works.import.meta.dirnameandimport.meta.filenameshould probably be avoided because they are not supported in older versions.It seems there was a bug and fixed by https://github.com/vitejs/vite/pull/16471 coincidentally.
@dhruvkaushik305
@prisma/client-generatedas instructed initially viaoutputin schema, this way the plugin you won’t have to deal with rebasing the path to.prisma/client@prisma/clientwith@prisma/client-generated__dirname/__filenamewithimport.meta.dirname/import.meta.filenameeval("__dirname")with justimport.meta.dirname, noeval()call, this was the one that was causing the error!eval("__dirname")works, buteval("import.meta.dirname")doesn’t, and I see no need for itThis is the only plugin needed now.
Possibly the only remaining problem that you might have during runtime is missing Prisma binary, for example
node_modules/@prisma/client-generated/libquery_engine-darwin-arm64.dylib.node. Figure out which one you need for Vercel and you could either add abuildEnd()hook to theprisma:buildplugin where you copy over the binary, or extend your.vercel/prepare.jsscript. I think it must be located directly in theindex.funcdirectory, outside ofindex.func/assets.If you’re still interested in pursuing this problem, let me know how it goes.
Wow that’s really helpful, thank you so much, I really appreciate your help and efforts ❤️
And considering Prisma’s very slow movement towards ESM I recomend to both of us that we try out Drizzle, if that’s a viable option for you.
I changed the output as
and also changed the import as
import { PrismaClient } from "@prisma/client-generated";While building vite said this
and the server logs on vercel have this error
Is there something that i am missing?
Not sure this is actionable for us either. Just to note, this repro is on Vite 5, but there is a prisma related issue with Vite 6 as well in https://github.com/remix-run/react-router/issues/12610. (Commented at the same time before seeing sapphi’s reply 😄)