Ever been stuck trying to get Prisma working in a brand-new Next.js 15 project using pnpm? I certainly was! It starts with a cryptic error, then leads to another, but don't worry – the fix is straightforward.
Let me describe the exact sequence of events I experienced and, more importantly, the simple solution.
The Initial Roadblock: "Cannot find module"
My journey began when I tried to run pnpm prisma init for the first time. Instead of the expected output, I was greeted with an error message like this:
Cannot find module 'C:\Users\ISHAIL~1\AppData\Local\Temp\@prisma\cli-init@latest-1748041200000\node_modules\@prisma\cli-init\dist\index.js' imported from C:\Users\Ishaili Paul C\AppData\Local\pnpm-cache\dlx\8f1b58d75bac52f467fe74fc409e2c5f30d82b5c62aa5c7aeba18d87aadf6e07\197039ad1d8-2588\node_modules\.pnpm\prisma@6.8.2\node_modules\prisma\build\index.js
This "Cannot find module" error means that Prisma's necessary command-line tools or core engine couldn't be located. This usually happens because a critical part of Prisma's installation—a "build script"—was skipped.
My first instinct (and often a good one for package manager issues) was to clean up:
rm -rf node_modules # Delete the node_modules folder
rm -f pnpm-lock.yaml # Delete the pnpm lock file
pnpm install # Re-run installation
This cleared the "Cannot find module" error, but immediately presented the real underlying issue in the form of a pnpm warning:
The Real Problem: Ignored Build Scripts Warning
After reinstalling, pnpm gave me this informative (but initially puzzling) message:
The following dependencies have build scripts that were ignored: @prisma/client, @prisma/engines, prisma, sharp, unrs-resolver
To allow the execution of build scripts for these packages, add their names to "pnpm.onlyBuiltDependencies" in your "package.json", then run pnpm rebuild
Why does this happen?
Packages like @prisma/client, @prisma/engines, prisma (the CLI tool itself), and sharp aren't just simple JavaScript files. They often contain native code or need to run specific setup steps (called "build scripts" or "postinstall scripts") to download binaries (like Prisma's database query engine) or compile code specific to your operating system.
pnpm
is designed for efficiency and strict dependency management. By default, it can be cautious about running these potentially complex build scripts during a standard pnpm install. While this can sometimes speed things up or prevent unexpected issues, for these specific packages, ignoring their build scripts means they won't function correctly. That's why you saw the "Cannot find module" error earlier – Prisma simply wasn't fully set up!
The Solution: Tell pnpm What to Build!
The fix is surprisingly simple and involves a quick configuration in your package.json file.
We need to explicitly tell pnpm to execute the build scripts for these essential packages.
Here's how to do it:
Open your package.json file.
You'll find this at the root of your Next.js project.
Add the pnpm.onlyBuiltDependencies configuration.
Insert a new top-level section named pnpm. Inside it, add an array called onlyBuiltDependencies. List every package that pnpm warned you about.
Your package.json should look similar to this (pay close attention to the new pnpm block at the bottom):
{
"name": "my-nextjs15-app",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"react": "^18",
"react-dom": "^18",
"next": "15.x.x", // Your Next.js 15 version
"@prisma/client": "5.x.x", // Your Prisma client version
"@prisma/engines": "5.x.x",// Essential for Prisma's core
"prisma": "5.x.x", // The Prisma CLI itself
"sharp": "^0.33.x", // For image processing
"unrs-resolver": "^x.x.x" // As per your warning
// ...your other dependencies
},
"devDependencies": {
// ...your dev dependencies
},
"pnpm": {
"onlyBuiltDependencies": [
"@prisma/client",
"@prisma/engines",
"prisma",
"sharp",
"unrs-resolver"
]
}
}
The pnpm block goes at the root level of your package.json, alongside name, version, dependencies, etc.
The package names in onlyBuiltDependencies must exactly match those listed in the pnpm warning message.
Run pnpm rebuild:
After saving your package.json file, open your terminal in the project's root and execute:
pnpm rebuild
This command specifically tells pnpm to re-run the build scripts for the packages you've now explicitly allowed. You should see output from Prisma downloading its engine and potentially sharp compiling native modules.
Finally, Initialize Prisma!
Once pnpm rebuild completes successfully, you can now run your Prisma initialization command without issues:
pnpm prisma init
Prisma should now successfully generate your schema.prisma file and .env file, getting you ready to define your database models.
Conclusion
The "Cannot find module" error and the pnpm build script warning are two sides of the same coin when it comes to packages like Prisma. By understanding that certain dependencies require explicit permission to execute their build steps, you can quickly resolve these installation headaches using pnpm.onlyBuiltDependencies.
This ensures all your native dependencies are correctly installed, allowing you to dive into building your Next.js 15 application with confidence.
Happy coding!
Top comments (0)