Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

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

Open
abereghici opened this issue Aug 31, 2021 · 10 comments
Assignees

Comments

@abereghici
Copy link

- 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
Copy link
Author

abereghici commented Sep 1, 2021

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/@prisma+client@2.30.2_prisma@2.30.2/node_modules/@prisma.

getDependenciesForModuleName for @prisma/client fails because

getNestedModules({ modulePath, state, packageJson, pluginsModulesPath }),

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.

@outerlook
Copy link

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
Copy link

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

@netlify-team-account-1
Copy link
Contributor

netlify-team-account-1 commented Sep 16, 2021

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

@abereghici
Copy link
Author

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

@netlify-team-account-1
Copy link
Contributor

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.

kodiakhq bot added a commit that referenced this issue Sep 29, 2021
* chore: add repro test for pnpm-style symlinking

* chore: show that it works with esbuild-based bundlers

Co-authored-by: Netlify Team Account 1 <netlify-team-account-1@users.noreply.github.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
@erlandsona
Copy link

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?

@Skn0tt
Copy link
Member

Skn0tt commented Jul 4, 2022

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 :)

@erlandsona
Copy link

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.

@bis0072
Copy link

bis0072 commented Mar 10, 2023

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

Skn0tt pushed a commit to netlify/build that referenced this issue May 21, 2024
…/zip-it-and-ship-it#657)

* chore: add repro test for pnpm-style symlinking

* chore: show that it works with esbuild-based bundlers

Co-authored-by: Netlify Team Account 1 <netlify-team-account-1@users.noreply.github.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants