prisma: `Uncaught TypeError: Error resolving module specifier “.prisma/client/index-browser”. Relative module specifiers must start with “./”, “../” or “/”.`
Bug description
I have the following error message in my browser upon using sveltekit and the command “npm run preview”:
Uncaught TypeError: Error resolving module specifier “.prisma/client/index-browser”. Relative module specifiers must start with “./”, “../” or “/”.
It references a piece of code that was compiled with “npm run build” in localhost:3000/_app/start-b07b1607.js:
...s-d1fb5791.js";import".prisma/client/index-browser";let Be="",et="";function ...
How to reproduce
I have tried reproducing this error with using older versions of Prisma, the adaptor and Svelte, switching from pnpm to npm, but nothing helps. I have a MWE repository that comes close to reproducing the error but doesn’t actually reproduce it at https://github.com/wvhulle/prisma-sveltekit-bug-report.
Expected behavior
How come the Svelte compiler emits “.prisma/client/index-browser” as a module specifier? Is this an error in Prisma, Vite or something else? The dev mode works without problem.
The question seems to be related, but is about Vue, not about Svelte.
Prisma information
I cannot share the schema, but this is the query that happens on the page where the error happens.
import prisma from '$lib/db';
export async function get() {
const projects = await prisma.project.findMany({})
return { body: projects }
}
And i am using this solution for the production build of Sveltekit:
import pkg, { PrismaClient } from '@prisma/client';
import { dev } from '$app/env';
declare global {
var _prisma: PrismaClient; // eslint-disable-line
}
let prisma;
if (dev) {
if (!global._prisma) {
global._prisma = new PrismaClient();
}
prisma = global._prisma;
} else {
const { PrismaClient: PrismaClientProd } = pkg;
prisma = new PrismaClientProd();
}
export default prisma as PrismaClient; // type assertion for shim
Environment & setup
- OS: Linux Mint 20.3
- Database: PostgreSQL
- Node.js version: v17.8.0
Prisma Version
prisma : 3.11.0
@prisma/client : 3.11.0
Current platform : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine b371888aaf8f51357c7457d836b86d12da91658b (at ../../../../.pnpm-store/v3/tmp/_npx/69216/5/node_modules/.pnpm/@prisma+engines@3.11.0-48.b371888aaf8f51357c7457d836b86d12da91658b/node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine : migration-engine-cli b371888aaf8f51357c7457d836b86d12da91658b (at ../../../../.pnpm-store/v3/tmp/_npx/69216/5/node_modules/.pnpm/@prisma+engines@3.11.0-48.b371888aaf8f51357c7457d836b86d12da91658b/node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine : introspection-core b371888aaf8f51357c7457d836b86d12da91658b (at ../../../../.pnpm-store/v3/tmp/_npx/69216/5/node_modules/.pnpm/@prisma+engines@3.11.0-48.b371888aaf8f51357c7457d836b86d12da91658b/node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary : prisma-fmt b371888aaf8f51357c7457d836b86d12da91658b (at ../../../../.pnpm-store/v3/tmp/_npx/69216/5/node_modules/.pnpm/@prisma+engines@3.11.0-48.b371888aaf8f51357c7457d836b86d12da91658b/node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Default Engines Hash : b371888aaf8f51357c7457d836b86d12da91658b
Studio : 0.458.0
Preview Features : fullTextSearch
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 20
- Comments: 70 (12 by maintainers)
Commits related to this issue
- try this: https://github.com/prisma/prisma/issues/12504#issuecomment-1147356141 — committed to stackabletech/feature-tracker by nikolai-cc 2 years ago
- fix(prisma): create alias https://github.com/prisma/prisma/issues/12504#issuecomment-1285883083 — committed to pdcstrrss/pdcstrrss by mrtnvh a year ago
- fix: correct error resolving module specifier major fix prisma/prisma#12504 — committed to dkantereivin/mfr-portal by deleted user a year ago
- fix: correct error resolving module specifier due to Prisma prisma/prisma#12504 — committed to dkantereivin/mfr-portal by deleted user a year ago
- fix: fix import enmu type in the frontend from prisma client https://github.com/prisma/prisma/issues/12504#issuecomment-1372632754 — committed to RayGuo-ergou/nuxt3-bar-web by RayGuo-ergou a year ago
- added prisma inline, migrate, migrate:dev and generate scripts from https://github.com/prisma/prisma/issues/12504 to silence vercel error — committed to ccozens/nursery-menu by ccozens 9 months ago
- updated export from export * from '.prisma/client' to export * from '../../.prisma/client' in node_modules/.pnpm/@prisma+client@5.3.1_prisma@5.3.1/node_modules/@prisma/client/index.d.ts as per https:/... — committed to ccozens/nursery-menu by ccozens 9 months ago
- added this fix https://github.com/prisma/prisma/issues/12504#issuecomment-1541963336 for prisma type error and it builds successfully locally — committed to ccozens/nursery-menu by ccozens 9 months ago
- fix(prisma): fix TypeError on prisma types in client (https://github.com/prisma/prisma/issues/12504) — committed to augustinbegue/bingehub by augustinbegue 7 months ago
- fix for https://github.com/prisma/prisma/issues/12504#issuecomment-1136126199 — committed to 0sumcode/0up by danhstevens 6 months ago
- more fun with https://github.com/prisma/prisma/issues/12504 — committed to 0sumcode/0up by danhstevens 6 months ago
- Using alias for an import of prisma in the client https://github.com/prisma/prisma/issues/12504#issuecomment-1978811356 — committed to vladimir1284/fleet-towit by vladimir1284 2 months ago
i got a solution and is working totally fine atleast for me. just add this to vite.config.ts
Workaround
I have a problem when you are trying to import a
@prisma/clientinto a client-bundle. Despite the error, it makes little sense, because the prisma still can not work in the browser.There is only one exception: Types. The prisma is difficult to tree-shaking, and if you import only one enum, it will try to include the whole
@prisma/clientin your bundle and runtime errors.The workaround is quite simple.
This could have been avoided if the prisma had written all the enums in separate files. Then we could import a specific enum directly from the prisma without any problems
Upd
For TypeScript 4.9 you may use
satisfiesand re-export prisma’s enumsok not the perfect solution but this works.
package.json
just copy
jsfiles from.prisma/clientto@prisma/clienteverytime you generate.Just in case, I’ve managed to get a simpler version of that hacky ^ but working solution (platform agnostic). That regex looked terrifying haha
Problem
ok the problem is in
@prisma/clientimport string is starting with.Solutions
@prisma/clientshould import.prisma/clientwith"../../.prisma/client"not".prisma/client".prismashould be renamed to something else, like_prismaAfter serving the built
index.htmlfile withexpress, I encountered several errors due to the lack of access to enums from prisma. I tried your suggested solution but it didn’t work because I am on Windows.Therefore, I came up with a more generic solution that addresses the issue with the slashes in pathname. I hope it works on other operating systems, would you mind checking it on your setup?
It’s not svelte-specific. I fot same issue with nuxt 3 in production build in heroku
Works with sveltekit that uses pnpm, can run build and preview commands and no errors 🎉 . Platform is mac intel, gonna deploy this to prod and see if it stays working on linux.
Of course, you can ask, but the answer is pretty obvious: Because we had and have other things to do. We can only work on so many things, and this did not make the top of the list. If another tool gives you all you need, then of course you should use that.
Can I ask, why is this still an issue one year on from the original bug report? Can the priority for this be bumped? Currently on 4.11.0. Thinking about Drizzle ORM at this point …
I just wasted a bunch of time on this, huge bummer. Simply importing anything in the same file as a
Prisma.validatorcall from the client brings the entire thing to a halt with the following error in the browser (Firefox 121.0.1 AND Safari Version 17.1):The alias solution above works for me but seems like we shouldn’t have to do something like that… I have created a minimum reproduction repo @jharrell
https://github.com/diericx/prisma-validator-issue
Relevant package info from the repo above:
This seems to have resolved the issue for me, so big thanks to you @sharmapukar217! 🙏
Sure I’ll check it on morning. It’s midnight and my laptop is off so I’ll inform you if this works on Ubuntu or not 🙂
the workaround is simple by just setting the output path in
prisma.schemetonode_modules/@prisma/client/.prisma/client/without any copying.remember
primsa generateafter updating@prisma/clientpackage.1️⃣ Reproduce?
I cloned your repo but was not able to produce any error. you can with your reproduction repo? Or you stripped out too many things?
2️⃣ My issue was
I looked up in my git history, and I was wrong in my previous comment. The issue was happening when I was importing enums like:
To overcome this, I need to do it like:
Maybe it’s something similar that you have?
3️⃣ Prisma client for dev & preview
I’m using this way to init my client and it’s working well;
(Actually, I don’t use the default export but directly
prismaInstance. But this worked well also with your repo.)This does work.
Though many people use
.'.prisma/client/index-browser': './node_modules/.prisma/client/index-browser.js',I have to use@'.prisma/client/index-browser': './node_modules/@prisma/client/index-browser.js',Also,
pnpm run buildworks for me.This worked for me, thanks @nickyhajal! FWIW, I’m using prisma v5.3.1, no problems in dev but typeerror on local build and deploy to vercel. This fixed it.
I can confirmed that this is not fixed/resolved in 5.0 or later. THis has been an issue for us running
5.3.1We’re having the same issue using Nuxt3 + prisma
5.3.1so I can confirm the issue is still present.I am under the gun with a deadline right now, but I will make a note to see if I can try to reproduce this for you this weekend.
Not quite, perhaps that repo is not quite the reproduction we’re looking for. I haven’t had time today to work on the reproduction repository, but I will have if ready soon.
We essentially need a vite frontend setup, and inside that setup if we import, e.g., Prisma and use Prisma.Decimal.isDecimal() it will show this error. For me, I was creating a tRPC transformer for handling decimals, and the same transformer is needed on the front end as the back end for type inference and serialisation to work correctly.
In issue I have found, is even if I import from a project that has prisma listed as a dependency into a vite project - I see this error. I can only import types that get stripped at compilation / build time.
I’ll have more information soon.
worked for me. thank you!
Any update on this? Still having issues.
@devi4nt @Chadyka do you have reproduction repos available? That would really help us understand what we missed in our fix. Any reproduction you can give us would be immensely helpful.
This was not an issue in my project using vite@4.1.4 with prisma@5.0.0. I only upgraded vite to @4.4.7 and this issue appeared. Still w prisma@5.0.0.
@kuubson 's workaround worked flawlessly for me, and is a simple workaround until the issue is resolved, probably from vite’s end. Much thanks to them and the other helpers.
Lots of people are having this problem, with the solution I found being so different from the other ones presented, I’m as baffled as everybody else but will share my experience debugging this in case it helps unearth some arcane glitch or identify a point that needs to be made clearer in the documentation, perhaps not even as a description of Prisma’s behavior as much as a “tons of people get this wrong, here’s a common pitfall to avoid” footnote. I spent this morning, yesterday, and the day before trying to figure out what was causing this issue for me. The issue:
npm run dev = runs flawlessly. Pushing to github → Vercel, with package.json scripts I tested in two configurations (C, I tried after I found the solution): A
B
C
and no modifications of vercel’s defaults, however, led to two pages breaking as pictured:
I am using Nuxt 3.3.2 and Prisma 4.12. I had this problem with two pages on my site not working, pages/…/pic/[picTitle] and /pages/tag/[tagName]. I have SSR set to false for tag/** and pic/**. Into the broken two pages, I was importing a couple validation functions from a file /types/models.ts that imported
and has a couple Prisma.validators to determine the types. I also exported a type-validating function from this file.
I exported this blog type and another to a page that worked, and art types to a page that didn’t. For some reason, although I have very similar logic between my blog post page and my art page, the art and tag pages were breaking yet not my blog. Weirder still is the tag description page, which wasn’t working, uses a much simpler function:
I tried messing with a lot of the page SFC typescript, since @Prisma and .prisma seemed likely culprits and the pages were what weren’t working. But when I started to realize that there is magic happening between the two aforementioned prisma folders, and @Prisma is supposed to be the “official” library, which then exports stuff from the generated library, I wondered if at some step in the build, maybe at some point in building/generating process, code is being run in a context where it doesn’t have the right access to certain references. One thing my two broken pages had in common, in contrast to my other pages using the code here, was that I was not just using exported types, but calling these functions. These are the only times that I’m executing code from this file, where Prisma.ArtArgs is referenced, with Prisma being imported from @prisma rather than .prisma. Perhaps at the time where vite (common between nuxt 3 and sveltekit) is building the page, it has lost or not yet made a connection between the @prisma npm package and generated code in .prisma. I suspect it’s “not yet” because the deployment process involves clean installs and has to then generate stuff for @prisma to reference.
Testing this theory gave positive evidence. My pages are no longer broken (commit that fixed it) after simply copying and pasting what Intellisense generated to replace the validator functions and subsequent
Prisma.ModelGetPayload<typeof modelValidator>convention that is suggested in the Prisma documentation. I didn’t even have to change the type-validating function I was exporting after I swapped the earlier type-generating code out for this:What I think we collectively need a better understanding of and ramification explanation for is: what code one can access in what contexts at what stages of build/runtime. I didn’t see anybody on this thread mention what order their build script Perhaps the documentation could suggest that developers use these validator functions to generate the types, but replace them the types generated by Intellisense. If you are using Prisma.[generated code] but you’re importing from the npm package @prisma, it seems like one needs to be mindful of when the .prisma client is generated, which must happen for its generated features (e.g. Prisma.YourCustomModelArgs) to be usable by @prisma.
This is when I tested what would happen if I switched up the order of the build command (i.e. `“nuxt build && prisma generate” → “prisma generate && nuxt build”) and reverted my code. This returned it to a broken state!
Hopefully this helps somebody. If you’re getting this error, look at whether there are files where the frontend might call code from files where you have:
Surely ongoing bugs and fixes should be priority for your users??
@shtse8 if my memory is not failing me, i tried doing that before sending my solution. it didnt work for me but gonna look into it again