zip-it-and-ship-it icon indicating copy to clipboard operation
zip-it-and-ship-it copied to clipboard

Error: Cannot find module '@prisma/client' in netlify functions when using prisma + next + rushjs + pnpm

Open abereghici opened this issue 2 years ago • 10 comments

- Do you want to request a feature or report a bug?

🐞

- What is the current behavior?

In a rush monorepo with pnpm @prisma/client and .prisma files aren't included in function node_modules and throw the error:

{
errorType: "Runtime.ImportModuleError",
errorMessage: "Error: Cannot find module '@prisma/client' Require stack: - /var/task/.netlify/functions-internal/next_api_views/next_api_views.js - /var/task/next_api_views.js - /var/runtime/UserFunction.js - /var/runtime/index.js",
trace: [
"Runtime.ImportModuleError: Error: Cannot find module '@prisma/client'",
"Require stack:",
"- /var/task/.netlify/functions-internal/next_api_views/next_api_views.js",
"- /var/task/next_api_views.js",
"- /var/runtime/UserFunction.js",
"- /var/runtime/index.js",
" at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
" at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
" at Object.<anonymous> (/var/runtime/index.js:43:30)",
" at Module._compile (internal/modules/cjs/loader.js:999:30)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)",
" at Module.load (internal/modules/cjs/loader.js:863:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:708:14)",
" at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)",
" at internal/main/run_main_module.js:17:47"
]
}

- If the current behavior is a bug, please provide the steps to reproduce.

  • Create a rush monorepo with pnpm as package manager.
  • Add a nextjs app with prisma inside of monorepo
  • Set next target to experimental-serverless-trace
  • Run prisma generate && netlify-cli build && netlify-cli deploy
  • Unzip a function that uses prisma from .netlify/functions/ and see that @prisma/client and .prisma are not included in node_modules

If you change the package manager from pnpm to npm then is working as expected.

I created a repository where you can reproduce the issue: https://github.com/abereghici/prisma-next-netlify-rush

- What is the expected behavior? @prisma/client and .prisma files should be included in node_modules when using pnpm.

- Please mention your node.js, and operating system version.

System:
    OS: macOS 10.15.7
    CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
    Memory: 372.44 MB / 32.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 14.17.2 - /usr/local/bin/node
    Yarn: 1.22.0 - /usr/local/bin/yarn
    npm: 6.14.13 - /usr/local/bin/npm

abereghici avatar Aug 31 '21 17:08 abereghici

I spent some time debugging this issue and here are my findings:

Rush with PNPM doesn't install nested dependencies but prisma depends on

"dependencies": {
    "@prisma/engines-version": "2.30.1-2.b8c35d44de987a9691890b3ddf3e2e7effb9bf20"
  }

so, in node_modules of the project we have only "@prisma/client" because is symlinked by pnpm. @prisma/engines-version is not a direct dependency so it lives somewhere in common/temp/node_modules/.pnpm/@[email protected][email protected]/node_modules/@prisma.

getDependenciesForModuleName for @prisma/client fails because https://github.com/netlify/zip-it-and-ship-it/blob/cfec60f66d8eea2023bea3dad0fcd7afaa3a09c0/src/node_dependencies/traverse.js#L120

cannot find @prisma/engines-version in node_modules of the project and in the end all @prisma dependencies are skipped.

A potential fix is to manually add @prisma/engines-version along with @prisma/client in package.json.

abereghici avatar Sep 01 '21 21:09 abereghici

Yay, sorry but happy to see this issue. I'm getting this too.

If I may add, local netlify dev works perfectly. Only when uploading, the problem arises. In my case, if I try to add --shamefully-hoist to install option, the error displayed complains about lack of nexus-plugin-prisma module.

But while using pnpm monorepo, it ain't so easy to go back to npm structure

outerlook avatar Sep 01 '21 23:09 outerlook

I almost created another minimal repro without rush that self contains a SQLite file as database to test.

But I didn't get it to work on Netlify, just locally. (the problem here is I don't know how to preserve the file for prisma to be able to read)

Anyway, the structure is the same as I get errors, if someone can give any idea to create this self-contained repro that works on netlify online. Or if this is already enough for the team to reproduce the error with a Database of your own online.

https://github.com/outerlook/netlify-prisma-nexus

outerlook avatar Sep 03 '21 18:09 outerlook

Hi @abereghici! Thank you for providing a reproduction repository. It seems like apps/next-app is a symlink, is that intended?

netlify-team-account-1 avatar Sep 16 '21 07:09 netlify-team-account-1

@netlify-team-account-1 Sorry, there was a misconfiguration. I updated the repository.

abereghici avatar Sep 16 '21 09:09 abereghici

Thank you @abereghici! I think I was able to reproduce the error based on https://github.com/outerlook/netlify-prisma-nexus, I'll use your repository to verify a fix once we have it.

netlify-team-account-1 avatar Sep 16 '21 09:09 netlify-team-account-1

Sorry to necro bump but is there a fix for this for regular npm too? I'm running into this and would love a way to debug / a fix potentially?

erlandsona avatar Jun 30 '22 14:06 erlandsona

Not sure what issue you're referring to - if there's already an issue for it in this repository, could you link it? If not, feel free to open one :)

Skn0tt avatar Jul 04 '22 13:07 Skn0tt

Turns out it's likely not this... and more to do with a js module trying to execute an import statement like an mjs module. My b.

erlandsona avatar Jul 04 '22 22:07 erlandsona

Runtime.ImportModuleError - Error: Cannot find module '@prisma/client' Require stack: - /var/task/functions/allposts.js - /var/task/allposts.js - /var/runtime/index.mjs

bis0072 avatar Mar 10 '23 18:03 bis0072