Error resolving module specifier “.prisma/client/index-browser”.
Describe the bug
I have the following error message in my browser upon using sveltekit (adaptor-node) 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 ...
However, it is unclear why this piece of code was inserted by npm run build. Is there a way to remove it?
Reproduction
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.
Logs
`Uncaught TypeError: Error resolving module specifier “.prisma/client/index-browser”. Relative module specifiers must start with “./”, “../” or “/”.`
System Info
System:
OS: Linux 5.14 Linux Mint 20.3 (Una)
CPU: (8) x64 11th Gen Intel(R) Core(TM) i5-1145G7 @ 2.60GHz
Memory: 4.41 GB / 15.37 GB
Container: Yes
Shell: 3.4.0 - /usr/bin/fish
Binaries:
Node: 17.8.0 - /usr/bin/node
npm: 8.5.5 - /usr/bin/npm
Browsers:
Chromium: 99.0.4844.82
Firefox: 98.0.2
npmPackages:
@sveltejs/adapter-node: next => 1.0.0-next.73
@sveltejs/kit: next => 1.0.0-next.303
svelte: ^3.46.4 => 3.46.4
Severity
blocking all usage of SvelteKit
Additional Information
No response
What does "comes close" mean? We'll need a reproduction of what you're seeing in order to investigate anything.
I understand, I am still trying to reproduce in the MWE what I am seeing in the actualy build.
Okay I found it, there is a problem when you are using Prisma and import an enum type.
The code that was the problem was not in the MWE, but was contained in the fragment
import { Configuration } from '@prisma/client';
export function toObject(formData) {
// console.log("formData", formData)
const object: any = {};
formData.forEach((value, key) => {
console.log(`key: ${key}, value: ${value}`);
if (value === "series") {
object[key] = Configuration["series"]
} else if (value === "parallel") {
object[key] = Configuration["parallel"]
} else if (!isNaN(Number(value)) && Number(value) !== undefined) {
object[key] = Number(value)
} else if (value !== "undefined" && value !== undefined && value !== "") {
object[key] = value
}
}
);
return object
}
and more specifically import { Configuration } from '@prisma/client';. Removing this import solved my problem.
Closing as there's nothing we can do here - vite doesn't support enums, because esbuild doesn't. Libraries generally shouldn't be distributing code with enums IMHO
this is not an enum issue, node_modules/.prisma contains generated js files. index.js and index-browser.js doesn't have real enums, they only have objects mapping the enums.
My not too elegant solution: https://github.com/prisma/prisma/issues/12504#issuecomment-1136126199
Uncaught TypeError: Failed to resolve module specifier "@prisma/client". Relative references must start with either "/", "./", or "../".
this is not an
enumissue,node_modules/.prismacontains generated js files.index.jsandindex-browser.jsdoesn't have real enums, they only have objects mapping the enums.My not too elegant solution: prisma/prisma#12504 (comment)
This worked for me (when using vite): https://github.com/prisma/prisma/issues/12504#issuecomment-2505171223
Add '.prisma/client/index-browser': './node_modules/.prisma/client/index-browser.js', as a Vite Alias
Add
'.prisma/client/index-browser': './node_modules/.prisma/client/index-browser.js',as a Vite Alias
Like to note, this doesn't work for pnpm.
The solution mentioned here will help: https://github.com/prisma/prisma/issues/12504#issuecomment-2608725259
Otherwise, builds will fail.