prisma / prisma

Next-generation ORM for Node.js & TypeScript | PostgreSQL, MySQL, MariaDB, SQL Server, SQLite, MongoDB and CockroachDB
https://www.prisma.io
Apache License 2.0
38.67k stars 1.51k forks source link

When using `pnpm` and deploying Prisma Client to Heroku, it fails at runtime with `Cannot find module '.prisma/client'` #24199

Open Jolg42 opened 3 months ago

Jolg42 commented 3 months ago

Bug description

Here is the error, from our platforms (heroku, library, ubuntu-20.04) test. https://github.com/prisma/ecosystem-tests/actions/runs/9091632780/job/24986713804#step:8:4114

2024-05-15T07:36:15.186332+00:00 app[web.1]: > heroku@1.0.0 start
2024-05-15T07:36:15.186333+00:00 app[web.1]: > node index.js
2024-05-15T07:36:15.186333+00:00 app[web.1]: 
2024-05-15T07:36:15.297904+00:00 app[web.1]: Example app listening on port 45358!
2024-05-15T07:36:15.599760+00:00 heroku[web.1]: State changed from starting to up
2024-05-15T07:36:17.599044+00:00 app[web.1]: node:internal/modules/cjs/loader:1145
2024-05-15T07:36:17.599061+00:00 app[web.1]: const err = new Error(message);
2024-05-15T07:36:17.599065+00:00 app[web.1]: ^
2024-05-15T07:36:17.599065+00:00 app[web.1]: 
2024-05-15T07:36:17.599065+00:00 app[web.1]: Error: Cannot find module '.prisma/client'
2024-05-15T07:36:17.599066+00:00 app[web.1]: Require stack:
2024-05-15T07:36:17.599066+00:00 app[web.1]: - /app/index.js
2024-05-15T07:36:17.599072+00:00 app[web.1]: at Module._resolveFilename (node:internal/modules/cjs/loader:1145:15)
2024-05-15T07:36:17.599073+00:00 app[web.1]: at Function.resolve (node:internal/modules/helpers:190:19)
2024-05-15T07:36:17.599078+00:00 app[web.1]: at /app/index.js:12:53
2024-05-15T07:36:17.599079+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/.pnpm/express@4.19.2/node_modules/express/lib/router/layer.js:95:5)
2024-05-15T07:36:17.599080+00:00 app[web.1]: at next (/app/node_modules/.pnpm/express@4.19.2/node_modules/express/lib/router/route.js:149:13)
2024-05-15T07:36:17.599081+00:00 app[web.1]: at Route.dispatch (/app/node_modules/.pnpm/express@4.19.2/node_modules/express/lib/router/route.js:119:3)
2024-05-15T07:36:17.599081+00:00 app[web.1]: at Layer.handle [as handle_request] (/app/node_modules/.pnpm/express@4.19.2/node_modules/express/lib/router/layer.js:95:5)
2024-05-15T07:36:17.599082+00:00 app[web.1]: at /app/node_modules/.pnpm/express@4.19.2/node_modules/express/lib/router/index.js:284:15
2024-05-15T07:36:17.599082+00:00 app[web.1]: at Function.process_params (/app/node_modules/.pnpm/express@4.19.2/node_modules/express/lib/router/index.js:346:12)
2024-05-15T07:36:17.599083+00:00 app[web.1]: at next (/app/node_modules/.pnpm/express@4.19.2/node_modules/express/lib/router/index.js:280:10) {
2024-05-15T07:36:17.599083+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',
2024-05-15T07:36:17.599083+00:00 app[web.1]: requireStack: [ '/app/index.js' ]
2024-05-15T07:36:17.599083+00:00 app[web.1]: }
2024-05-15T07:36:17.599086+00:00 app[web.1]: 
2024-05-15T07:36:17.599090+00:00 app[web.1]: Node.js v20.13.1

How to reproduce

See https://github.com/prisma/ecosystem-tests/tree/dev/platforms/heroku

Expected behavior

It should work (it was working before)

Prisma information

Environment & setup

Prisma Version

5.15.0-dev.5 for example, but all versions should be affected as it's not a regression from Prisma's side but a change in the buildpack used by Heroku.

Jolg42 commented 3 months ago

See internal Slack thread

One important thing to note, is that our current test has NPM_CONFIG_PRODUCTION=false & NODE_MODULES_CACHE=false set on Heroku as env vars.

After investigating, it looks like the Pruning devDependencies step of the heroku/nodejs buildpack changed, and is not skipped anymore.

A deployment using heroku/nodejs buildpack version 244, where runtime is working:

heroku buildpacks:set https://github.com/heroku/heroku-buildpack-nodejs.git#v244 -a name-of-app
....
remote: -----> Pruning devDependencies
remote:        Skipping because NPM_CONFIG_PRODUCTION is 'false'
remote:        

remote: -----> Compressing...
remote:        Done: 120.1M

A deployment using heroku/nodejs buildpack version 245, where runtime is failing with the Prisma Client error mentioned earlier:

heroku buildpacks:set https://github.com/heroku/heroku-buildpack-nodejs.git#v245 -a name-of-app

remote: -----> Pruning devDependencies
remote:        
remote:        devDependencies:
remote:        - @types/node 16.18.97
remote:        - heroku 8.11.5
remote:        - prisma 5.14.0-dev.74
remote:        - ts-node 10.9.2

remote: -----> Compressing...
remote:        Done: 89.7M

The changelog https://github.com/heroku/heroku-buildpack-nodejs/blob/HEAD/CHANGELOG.md Shows that v245 added support for pnpm -> https://github.com/heroku/heroku-buildpack-nodejs/pull/1224

Looking at https://github.com/heroku/heroku-buildpack-nodejs/blob/a409b633d7925c3002ba4568bc833093cbd75237/lib/dependencies.sh#L338 It looks like the behavior is not the same for pnpm as for yarn and npm and so this behavior is expected at the moment as the env vars are not used in that newer code path.

Jolg42 commented 3 months ago

Current workaround: using a custom out path works. Example

generator client {
  provider = "prisma-client-js"
  output   = "../src/generated/client"
}

See https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/generating-prisma-client#using-a-custom-output-path