keystone icon indicating copy to clipboard operation
keystone copied to clipboard

Postinstall hook throws Prisma error when using pnpm: Command failed with exit code 1: npm install -D prisma@latest

Open isla-nye opened this issue 3 years ago • 10 comments

  • Use pnpm for dependency management.
  • Run keystone postinstall

Expected behaviour: Postinstall completes

Actual: Error thrown when trying to install Prisma, see below screenshot (it's for postinstall --fix but the same thing happens with postinstall): image

This seems like it's probably an issue with the dependency file structure of pnpm. No such error happens with npm, or when forcing a flat node_modules structure by using pnpm i --shamefully-hoist.

It's not clear to me whether this is an issue on the Prisma side or on the Keystone side. Please let me know if it needs to be reported elsewhere.

Node version 18.3

isla-nye avatar Jun 13 '22 20:06 isla-nye

Hi @isla-nye Thanks for posting, we are exploring how we might better support pnpm for package management, currently you can list prisma and @prisma/client in your dependencies OR as you suggest use pnpm i --shamefully-hoist. Thanks

borisno2 avatar Jun 21 '22 05:06 borisno2

For me I see a lot more errors. Like I have to specify react and tons of other libraries in my package.json when trying to use pnpm out of the box. I am trying to use pnpm and pnpm workspaces. So maybe my issue is also related to workspaces as I had no luck with yarn workspaces ether.

fkrauthan avatar Jun 22 '22 06:06 fkrauthan

To be more specific I had to list:

  • react
  • @babel/runtime
  • @prisma/client
  • prisma
  • @types/react
  • @types/node

That at least makes all keystone commands succeed and the GraphQL API works as expected. BUT when trying to access the keystone admin UI I am stuck with the error:

Error: Cannot find module 'next/dist/shared/lib/constants.js'
Require stack:
- C:\Users\fkrauthan\WebstormProjects\xxx\backend\.keystone\admin\.next\server\pages\_document.js

fkrauthan avatar Jun 23 '22 01:06 fkrauthan

@fkrauthan We use pnpm workspaces too. --shamefully-hoist for just the package using KeystoneJS seems to work fine as a temporary workaround without having to add other dependencies, though I understand if you don't want to use it as a long-term solution.

isla-nye avatar Jun 23 '22 08:06 isla-nye

@isla-nye how do I enable the --shamefully-hoist just for the backend? Or do I always need to remember to call pnpm i --shamefully-hoist?

fkrauthan avatar Jun 23 '22 16:06 fkrauthan

@fkrauthan you just have to remember pnpm i --shamefully-hoist (for the package containing your Keystone app only - no need to have it for the other packages in your workspace), and also remember to put it in whenever you run pnpm i in your CI scripts.

isla-nye avatar Jun 24 '22 08:06 isla-nye

But how does that work if I run pnpm i from the root of my project? It would loose the --shamefully-hoist install method for the backend subpackage wouldn't?

fkrauthan avatar Jun 24 '22 17:06 fkrauthan

~I ran into some problems today with @prisma/client not having access to the generated client located in .prisma/client as pnpm keystone postinstall --fix and pnpm keystone prisma generate wouldn't put it into the appropriate folder. This issue came up when I tried to upgrade pnpm from version 6.x to 7.x~

Edit: It actually wasn't 6.x vs 7.x., the problem was fixed by using pnpm prisma generate instead of pnpm keystone prisma generate, because the keystone command was instantiating our keystone.ts which relied on the @prisma/client dependency.

mmachatschek avatar Jul 01 '22 18:07 mmachatschek

I'm also seeing this issue as described by @fkrauthan when accessing the Admin UI

Error: Cannot find module 'next/dist/shared/lib/router/utils/remove-trailing-slash.js'

Im trying to avoid --shamefully-hoist as I have a different package in my monorepo which also uses NextJS that I'd like to have the versions kept sepearately.

xenobytezero avatar Sep 11 '22 15:09 xenobytezero

Also I can't add Next as a direct dependency like I normally do, as Keystone throws the usual "you have multiple Reacts" error

Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following 
reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app        
See https://reactjs.org/link/invalid-hook-call for tips about how to 
debug and fix this problem.

xenobytezero avatar Sep 11 '22 17:09 xenobytezero

So I hit this error again with pnpm and after some debugging I found out, that the main issue is that execa doesn't know in which .bin folder it should look to use the prisma binary.

The process.env.PATH only contains the node_modules/.bin folder. In that folder (if you didn't explicitly install prisma) the binary for prisma is missing, as its only linked in the node_modules/@keystone-6/core/node_modules/.bin folder.

What I don't know is, if this is an issue on keystone, execa (executes the prisma command) or pnpm side.

At least keystone would fix this if the node_modules/@keystone6-core/node_modules/.bin path would be added to the command here I guess:

https://github.com/keystonejs/keystone/blob/6c60565c940a9e27b8bb9b3e3e61260f22ee1e1a/packages/core/src/scripts/prisma.ts#L15

Edit: I currently run keystone with pnpm without using --shamefully-hoist

mmachatschek avatar Oct 05 '22 14:10 mmachatschek

I have a similar problem as @mmachatschek when running pnpm install --shamefully-hoist it fails since its trying to resolve all lists where one of them does rely on the prisma client

> keystone postinstall

Error: Cannot find module '.prisma/client/index'
Require stack:
- /Users/user/git/keystone-project/node_modules/.pnpm/@[email protected][email protected]/node_modules/@prisma/client/index.js
- /Users/user/git/keystone-project/ks/services/athlete-result-service.ts
- /Users/user/git/keystone-project/ks/schema/Athlete.ts
- /Users/user/git/keystone-project/schema.ts
- /Users/user/git/keystone-project/keystone.ts
- /Users/user/git/keystone-project/node_modules/.pnpm/@[email protected]/node_modules/@keystone-6/core/dist/requireSource-ff4e29c3.cjs.dev.js
- /Users/user/git/keystone-project/node_modules/.pnpm/@[email protected]/node_modules/@keystone-6/core/scripts/cli/dist/keystone-6-core-scripts-cli.cjs.dev.js
- /Users/user/git/keystone-project/node_modules/.pnpm/@[email protected]/node_modules/@keystone-6/core/scripts/dist/keystone-6-core-scripts.cjs.dev.js
- /Users/user/git/keystone-project/node_modules/.pnpm/@[email protected]/node_modules/@keystone-6/core/scripts/dist/keystone-6-core-scripts.cjs.js
- /Users/user/git/keystone-project/node_modules/.pnpm/@[email protected]/node_modules/@keystone-6/core/bin/cli.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/Users/user/git/keystone-project/node_modules/.pnpm/@[email protected][email protected]/node_modules/@prisma/client/index.js:2:6)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Object.newLoader [as .js] (/Users/user/git/keystone-project/node_modules/.pnpm/[email protected]/node_modules/pirates/lib/index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/Users/user/git/keystone-project/ks/services/athlete-result-service.ts:13:1)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Module._compile (/Users/user/git/keystone-project/node_modules/.pnpm/[email protected]/node_modules/pirates/lib/index.js:136:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Object.newLoader [as .ts] (/Users/user/git/keystone-project/node_modules/.pnpm/[email protected]/node_modules/pirates/lib/index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/Users/user/git/keystone-project/ks/schema/Athlete.ts:26:1)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Module._compile (/Users/user/git/keystone-project/node_modules/.pnpm/[email protected]/node_modules/pirates/lib/index.js:136:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Object.newLoader [as .ts] (/Users/user/git/keystone-project/node_modules/.pnpm/[email protected]/node_modules/pirates/lib/index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/Users/user/git/keystone-project/schema.ts:26:1)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Module._compile (/Users/user/git/keystone-project/node_modules/.pnpm/[email protected]/node_modules/pirates/lib/index.js:136:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Object.newLoader [as .ts] (/Users/user/git/keystone-project/node_modules/.pnpm/[email protected]/node_modules/pirates/lib/index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/Users/user/git/keystone-project/keystone.ts:13:1)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Module._compile (/Users/user/git/keystone-project/node_modules/.pnpm/[email protected]/node_modules/pirates/lib/index.js:136:24)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Object.newLoader [as .ts] (/Users/user/git/keystone-project/node_modules/.pnpm/[email protected]/node_modules/pirates/lib/index.js:141:7)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.requireSource (/Users/user/git/keystone-project/node_modules/.pnpm/@[email protected]/node_modules/@keystone-6/core/dist/requireSource-ff4e29c3.cjs.dev.js:93:18)
    at postinstall (/Users/user/git/keystone-project/node_modules/.pnpm/@[email protected]/node_modules/@keystone-6/core/scripts/cli/dist/keystone-6-core-scripts-cli.cjs.dev.js:684:54) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/user/git/keystone-project/node_modules/.pnpm/@[email protected][email protected]/node_modules/@prisma/client/index.js',
    '/Users/user/git/keystone-project/ks/services/athlete-result-service.ts',
    '/Users/user/git/keystone-project/ks/schema/Athlete.ts',
    '/Users/user/git/keystone-project/schema.ts',
    '/Users/user/git/keystone-project/keystone.ts',
    '/Users/user/git/keystone-project/node_modules/.pnpm/@[email protected]/node_modules/@keystone-6/core/dist/requireSource-ff4e29c3.cjs.dev.js',
    '/Users/user/git/keystone-project/node_modules/.pnpm/@[email protected]/node_modules/@keystone-6/core/scripts/cli/dist/keystone-6-core-scripts-cli.cjs.dev.js',
    '/Users/user/git/keystone-project/node_modules/.pnpm/@[email protected]/node_modules/@keystone-6/core/scripts/dist/keystone-6-core-scripts.cjs.dev.js',
    '/Users/user/git/keystone-project/node_modules/.pnpm/@[email protected]/node_modules/@keystone-6/core/scripts/dist/keystone-6-core-scripts.cjs.js',
    '/Users/user/git/keystone-project/node_modules/.pnpm/@[email protected]/node_modules/@keystone-6/core/bin/cli.js'
  ]
}

I fixed that by changing my package.json like so

    "postinstall": "prisma generate && keystone postinstall",

The install then runs through, however, now when I run pnpm dev I get endless recompiles:

event - compiled client and server successfully in 3.3s (1248 modules)
✅ Admin UI ready
wait  - compiling /api/__keystone_api_build (client and server)...
event - compiled successfully in 675 ms (248 modules)
wait  - compiling...
event - compiled client and server successfully in 113 ms (1341 modules)
wait  - compiling...
event - compiled client and server successfully in 86 ms (1341 modules)
wait  - compiling...
event - compiled client and server successfully in 88 ms (1341 modules)
wait  - compiling...
event - compiled client and server successfully in 70 ms (1341 modules)
wait  - compiling...
event - compiled client and server successfully in 69 ms (1341 modules)
wait  - compiling...
event - compiled client and server successfully in 64 ms (1341 modules)
wait  - compiling...
event - compiled client and server successfully in 62 ms (1341 modules)
wait  - compiling...
event - compiled client and server successfully in 64 ms (1341 modules)
wait  - compiling...
event - compiled client and server successfully in 61 ms (1341 modules)
wait  - compiling...
event - compiled client and server successfully in 65 ms (1341 modules)

rburgst avatar Oct 12 '22 05:10 rburgst

ok, the issue with the constant recompiling seems to be #7902. I can confirm that setting the resolution as described in https://github.com/keystonejs/keystone/issues/7902#issuecomment-1243101439 fixes it for pnpm as well.

rburgst avatar Oct 12 '22 16:10 rburgst

Actually I was too fast, now after getting the install fixed, keystone wont run properly, I am now seeing this issue: https://github.com/keystonejs/keystone/issues/6848

rburgst avatar Oct 12 '22 17:10 rburgst

Actually I was too fast, now after getting the install fixed, keystone wont run properly, I am now seeing this issue: #6848

I also just had this same issue come up ^

TheRealFlyingCoder avatar Oct 14 '22 01:10 TheRealFlyingCoder

Cannot reproduce with https://github.com/keystonejs/keystone/releases/tag/2023-02-23

dcousens avatar Feb 28 '23 23:02 dcousens

Woohoo!!!

TheRealFlyingCoder avatar Mar 01 '23 01:03 TheRealFlyingCoder