prisma / prisma-client-js

Type-safe database client for TypeScript & Node.js (ORM replacement)
Apache License 2.0
1.47k stars 67 forks source link

Prisma Client removed on adding a new package: Error: @prisma/client did not initialize yet #390

Closed divyenduz closed 4 years ago

divyenduz commented 4 years ago

This behavior can be observed in both npm and yarn, setup a Photon project and run any query with photon to ensure that it exists.

Now add any dependency in yarn, npm and re-execute photon query. I reproduced this with yarn add dotenv or npm install dotenv.

It doesn't matter if dotenv is already install or is a new package. I could reproduce this reliably.

You would run into (error paraphrased):

npm: import { Photon } from '@prisma/photon' no found. yarn: Photon is not initialized yet. Please run prisma2 generate again

I used the following versions:

divyendusingh [prisma]$ yarn --version
1.21.1
divyendusingh [prisma]$ npm --version
6.10.2
timsuchanek commented 4 years ago

I can't reproduce this with npm@6.13.4 and yarn@1.21.1.

divyenduz commented 4 years ago

I could reproduce this once today. But not anymore afterwards. I would love to keep this in the next sprint as I think it is important and try to find a reproduction.

divyenduz commented 4 years ago

Tim has a reproduction of a different error of similar nature now. I will look for a related reproduction of the actual error "Photon is not initialized yet. Please run prisma2 generate again" but Tim can already start on the other bit of it.

divyenduz commented 4 years ago

"Photon is not initialized yet. Please run prisma2 generate again" this is also observable on Heroku. Might be related, will create a separate issue, if needed.

timsuchanek commented 4 years ago

I created a reproduction attempt in https://github.com/timsuchanek/photonjs-390. But just an attempt. Unassigning myself until we have a reliable reproduction.

pantharshit00 commented 4 years ago

I am also unfortunately unable to reproduce this.

divyenduz commented 4 years ago

I am also unable to reproduce this with a local package like in Tim's repo. I tried that and its file variant (referring to prisma-client by its folder).

janpio commented 4 years ago

Note: Update issue title and description to new output.

divyenduz commented 4 years ago

Internal note: Action item: "Change the facade message to link back to this issue". This would give us more visibility into how many people face this issue.

embiem commented 4 years ago

I encountered this issue after updating from preview 19 to 20.3

Happened locally, but also on the ZEIT now deployment. Could only fix it by adding the "postinstall": "prisma2 generate" script. AFAIK this should not be necessary and was one of the reasons for the @photon/client "fake" package, right?

timsuchanek commented 4 years ago

Thanks for letting us know @embiem. You're right, that the main reason to introduce @prisma/client was indeed to not need a postinstall hook anymore. However, because of https://github.com/yarnpkg/yarn/issues/7762 this is still a problem in Now. We have a "fix" in place, which installs a postinstall hook with "prisma2 generate" to the application if you deploy to Now, however, it seems like that "fix" is not working in your case. Two questions:

  1. Can you try it with the latest version preview 21?
  2. If it still happens, can you give us an overview about your folder structure, mostly if it's a mono repo and where you have the prisma/schema.prisma file and where you have the package.json.

Thanks!

embiem commented 4 years ago

I removed the postinstall script & updated to preview 21. Now, it works locally and also when deploying to ZEIT's now. Thank you!

My folder structure looked like this (no monorepo):

prisma
    migrations
    schema.prisma
package.json
krzkaczor commented 4 years ago

Just happened to me. In my project, I renamed prisma to database so my folder structure is non-standard.

I think it's obvious that it doesn't work without some kind of config file specifying where to find schema.prisma file. Can it be configured this way? For now I just added custom postinstall hook:

"postinstall": "yarn prisma:generate",
"prisma:generate": "prisma generate --schema=./database/schema.prisma",
krzkaczor commented 4 years ago

Oh, I just realized that it's possible to hit this bug due to a version mismatch:

  1. monorepo with yarn workspaces
  2. package-a has dependency on @prisma/cli and @prisma/client 2.0.0-beta.2
  3. package-b has dependency on @prisma/cli and @prisma/client 2.0.0-beta.3

I was getting this error all the time for one of these packages.

timsuchanek commented 4 years ago

Thanks for the info @krzkaczor! Can you try this with the latest alpha in both packages? We did recent fixes, which should alleviate this situation.

timsuchanek commented 4 years ago

We're closing this for now, as we can't reproduce this anymore. In case it's not fixed for you, just let us know and we can reopen it!

piesrtasty commented 4 years ago

@timsuchanek Can we reopen this issue as I'm getting it with "@prisma/client": "^2.0.0-beta.4", deploying to vercel / zeit

020-05-10T23:19:12.070Z undefined   ERROR   Error: @prisma/client did not initialize yet. Please run "prisma generate" and try to import it again.
In case this error is unexpected for you, please report it in https://github.com/prisma/prisma-client-js/issues/390.
    at new PrismaClient (/var/task/backend/node_modules/.prisma/client/index.js:3:11)
    at Object.<anonymous> (/var/task/backend/src/index.js:563:16)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/var/task/___now_launcher.js:21:18)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at _tryRequire (/var/runtime/UserFunction.js:75:12)
    at _loadUserApp (/var/runtime/UserFunction.js:95:12)
    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)
    at Object.<anonymous> (/var/runtime/index.js:45:30)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
RequestId: bfa7224f-573f-45f7-90e1-e9133d5e85c9 Error: Runtime exited with error: exit status 1
Runtime.ExitError

The latest version I can get to work on Zeit / Vercel is

"@prisma/cli": "2.0.0-beta.2",
"@prisma/client": "2.0.0-beta.2",

Both beta.3 and beta.4 result in the above error.

pantharshit00 commented 4 years ago

@lukehamilton Can you please a small reproduction? Thanks!

piesrtasty commented 4 years ago

@pantharshit00

I ended up having to add

"postinstall": "yarn prisma:generate",
"prisma:generate": "prisma generate --schema=./prisma/schema.prisma",

to my package.json to get it to work. It seems like that should not be needed?

jjjuk commented 4 years ago

@lukehamilton Might you show an example of server with prisma 2.0.0-beta.2 that works on Vecel?

piesrtasty commented 4 years ago

@jjjuk Here is an example that works on Vercel with beta4.

https://github.com/lukehamilton/prisma-beta4-reproduction

Hope it helps.

jjjuk commented 4 years ago

@lukehamilton thank you! It will be useful, but does it work with SQLite?

I've just deployed graphql server with prisma by myself, but It doesn't work with SQLite, I moved my database to postgresql like in your example.

piesrtasty commented 4 years ago

@jjjuk I'm not sure how to run it with SQLite on Vercel. I have an Postgres database running on Amazon that I'm using.

To use SQLite change the connector. I think you can do something like this...

datasource db {
  provider = "sqlite"
  url      = "file:./dev.db"
}
timsuchanek commented 4 years ago

Thanks for the reproduction @lukehamilton! We'll look into it.

@jjjuk Regarding SQLite and Vercel: As Vercel uses Lambda and Lambda does not allow writing to the app dir, but only to the /tmp dir, you need to copy or move your sqlite db on bootup time of your app to /tmp, then it works. I created a hacky example here, how one could do that: https://github.com/timsuchanek/next-prisma-now-sqlite

But using Postgres is for sure the recommended way :)

jjjuk commented 4 years ago

@timsuchanek Thank you so much!

2color commented 4 years ago

I just tried to deploy the Prisma Vercel example to Vercel and I'm seeing the same error. This worked up until recently, so I think it might be related to a change on Vercel's end.

Jolg42 commented 4 years ago

Indeed I can reproduce on Vercel with Daniel's example

[GET] /api/posts
16:32:36:59
Status:-1
Duration:178.88ms
Memory Used:15 MB
ID:tn878-1590417156107-ea98747663ef
User Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:77.0) Gecko/20100101 Firefox/77.0
2020-05-25T14:32:37.607Z    undefined   ERROR   Error: @prisma/client did not initialize yet. Please run "prisma generate" and try to import it again.In case this error is unexpected for you, please report it in https://github.com/prisma/prisma-client-js/issues/390.    at new PrismaClient (/var/task/node_modules/.prisma/client/index.js:3:11)    at Object.<anonymous> (/var/task/api/getPosts.js:2:16)    at Module._compile (internal/modules/cjs/loader.js:1156:30)    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)    at Module.load (internal/modules/cjs/loader.js:1000:32)    at Function.Module._load (internal/modules/cjs/loader.js:899:14)    at Module.require (internal/modules/cjs/loader.js:1042:19)    at require (internal/modules/cjs/helpers.js:77:18)    at Object.<anonymous> (/var/task/___now_launcher.js:22:18)    at Module._compile (internal/modules/cjs/loader.js:1156:30)RequestId: bb2de587-31b4-422c-9f6b-bd91932fac4d Error: Runtime exited with error: exit status 1Runtime.ExitError
janpio commented 4 years ago

e2e tests should have caught this, or we need to improve the e2e tests.

divyenduz commented 4 years ago

I am able to reproduce with the prisma-examples code but not with E2E code. My hunch is that the examples code uses /api from vercel and they might be deploying that differently from the node build method which E2E tests use.

@pantharshit00 or someone else, can you please confirm if this is also your observation?

To improve E2E tests, we might want to add another example for Vercel that uses the /api way of deployment, I can do that once this is confirmed.

pantharshit00 commented 4 years ago

I can also reproduce this with prisma-examples example: image

I can't wrap my head around how the e2e code is even deploying: https://github.com/prisma/e2e-tests/blob/master/platforms/vercel/index.js

It is not an serverless function, maybe vercel is shimming some behavior for compatibility.

pantharshit00 commented 4 years ago

The express example work! https://vercel-prisma-test-ptm2g0krt.now.sh/

Did vercel merge @now/node and @now/node-server into one? Anyways we need to test for serverless target in vercel

timsuchanek commented 4 years ago

Vercel is fixed again!

seanking11 commented 4 years ago

I am getting this same error when trying to deploy my functions to Firebase, even with the latest 2.0.0-beta.8. All of my functions use Prisma and I get the following error for each function that I try to deploy.

From the Firebase functions console:

Detailed stack trace: Error: @prisma/client did not initialize yet. Please run "prisma generate" and try to import it again.

Current Workaround The only way I've been able to deploy successfully is if I delete all of my functions and then deploy when nothing is already deployed. This seems to work as it cleanly installs & generates the Prisma client. If there is already code deployed, any subsequent changes will not work.

Versions "@prisma/cli": "^2.0.0-beta.8" "@prisma/client": "^2.0.0-beta.8" "firebase-functions": "^3.6.1" node - 10.16.3 npm - 6.9.0

Folder Structure I saw above that this could cause issues if you have a monorepo with different versions. My repo has both frontend and backend apps in it, however, @prisma/cli & @prisma/client are only installed in the backend portion functions. Here's my folder structure though in case that's helpful. When receiving the error above, I was in the functions folder attempting to deploy.

my-app
  |_ functions (backend)
    |_ src
    |_ node_modules
    |_ prisma
      |_ schema.prisma
      |_ .env
      |_ migrations
    |_ package.json
    |_ index.js (functions trying to get deployed, pulling code from `src`)
    |_ .nvmrc (10.16.3) (firebase functions don't support node 12 yet)
  |_ src
    |_ ... frontend code (Vue)
  |_ dist
  |_ package.json
  |_ .nvmrc (12.6.0)

I've also tried adding the following to my functions/package.json (as suggested above) to force firebase to re-generate the prisma client after installing, however, I've had no luck.

// package.json scripts
"postinstall": "npm run prisma:generate",
"prisma:generate": "npx prisma generate"

My current workaround works as the app isn't live, however, when I go to production soon that would require taking down our services for every code change, something I would really want to avoid. Please let me know if there's any other info I can provide to help debug this. Thanks Prisma team!

janpio commented 4 years ago

Can you please open a new issue @seanking11? We should investigate this properly, much easier in its own issue. Thanks.

seanking11 commented 4 years ago

@janpio I was opening up a new issue for this and rubber-ducked my way into finding the solution. The issue was that I had @prisma/cli listed as a devDependency and it seems like firebase does not install those. Therefore, when deploying, it would install all my dependencies (not prisma/cli) and then run the initial prisma generate, leaving me with the error I had outlined above.

I moved @prisma/cli to a normal dependency and tried deploying and it worked!

I was getting the following error and attempted the fix as outlined, but given what I mentioned above, that fix wouldn't work in Firebase's situation as it suggests adding as a dev dependency.

In order to use "@prisma/client", please install @prisma/cli. You can install it with "npm add -D @prisma/cli".

This seems to be pretty specific to my situation with Firebase, however, it might be helpful to note in the setup that services like Firebase might require the cli to be listed as a normal dependency.

Thanks for the quick response and for building an awesome product!

janpio commented 4 years ago

Awesome. We definitely want to debug or even fix this - so could you just copy your both comments here into a new issue anyway? Thanks.

Zhomart commented 4 years ago

There is https://github.com/prisma/prisma/discussions/2106 similar discussion going. I had similar issue, but it was my own fault adding **/node_modules/** to ignore field in firebase.json.

UPDATE: false alarm, use yarn instead of npm. You can still ignore node_modules.

Jolg42 commented 4 years ago

@janpio I created this issue for the Firebase problem https://github.com/prisma/prisma-client-js/issues/732

deadcoder0904 commented 4 years ago

I got this error Error: @prisma/client did not initialize yet. Please run "prisma generate" and try to import it again. when I updated Prisma to v2.2.0

janpio commented 4 years ago

Can you open a new issue pls @deadcoder0904? Thanks.

deadcoder0904 commented 4 years ago

I think I opened in wrong repo 🤦‍♂️ https://github.com/prisma/prisma/issues/2963

janpio commented 4 years ago

All good. Thanks.

nargetdev commented 4 years ago

@deadcoder0904 same here - exactly when I upgraded to "@prisma/client": "2.2.0" (which was necessary to fix a different bug within prisma up from 2.1.0)

deadcoder0904 commented 4 years ago

@nargetdev did you solve it? if not, here's the solution :)

MichaelGorski commented 4 years ago

I am getting this error. Couldn't find any workarounds yet.

Bildschirmfoto 2020-09-07 um 12 23 40

My dependencies

  "dependencies": {
    "@prisma/client": "^2.6.2",
    "apollo-server": "^2.17.0",
    "body-parser": "^1.19.0",
    "connect": "^3.7.0",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "faker": "^5.1.0",
    "firebase-admin": "^9.1.1",
    "nexus": "^0.26.1",
    "nexus-plugin-prisma": "^0.18.1",
    "uuid": "^8.3.0"
  },
  "devDependencies": {
    "@babel/cli": "^7.11.6",
    "@babel/core": "^7.11.6",
    "@babel/node": "^7.10.5",
    "@babel/preset-env": "^7.11.0",
    "@prisma/cli": "^2.6.2",
    "@types/node": "^14.6.4",
    "cross-env": "^7.0.2",
    "nodemon": "^2.0.4",
    "ts-node": "^9.0.0",
    "ts-node-dev": "^1.0.0-pre.62",
    "typescript": "^4.0.2"
  },
sreuter commented 4 years ago

@MichaelGorski You should remove your @prisma/cli and @prisma/client dependencies from your package.json and delete your node_modules (and yarn.lock if you use yarn) and install things fresh again.

That being said, I'm getting the same error right now, even tho I don't have those in my package.json.

MichaelGorski commented 4 years ago

Solved it Somehow when I installed nexus, my dependencies were bugged and it installed @prisma/client as a dependencie like @prisma/cli.

My Solution: Remove all related dependencies, delete node_modules. Install it clear. Remove all related prisma/nexus files from your project. Add nexus, run nexus dev. You should get a warning with empty Graphql etc.. Then move all the files back to api/graphql, add nexus-plugin-prisma, run nexus dev and it should work again.

@sreuter Did you try my solution? It worked for me

edit: grammar

sreuter commented 4 years ago

Thanks @MichaelGorski.. For me it was something else in case somebody still has problems:

I had to remove output = "./generated/client" from my schema.prisma (which was a left over from using the client directly before nexus), so npx prisma generate actually creates the client in the right location.

talentlessguy commented 3 years ago

Happens with pnpm 5.6 and node 14.8

 Error: @prisma/client did not initialize yet. Please run "prisma generate" and try to import it again

even though the client is already generated

doesn't work with yarn 1.22.5 either

even tho the client exists:

➜ ls node_modules/@prisma/client
 generator-build   node_modules   runtime
 index.d.ts        package.json   scripts
 index.js          README.md     
pantharshit00 commented 3 years ago

@talentlessguy Client is not generated at that location now, please check node_modules/.prisma