next.js ENOENT trying to open schema.prisma
Problem
Bug description In a monorepo, when exporting `PrismaClient` from one package and consuming it in a next.js api route, I get [code block] How to reproduce 1. Git clone https://github.com/SimonBerens/prisma-pnpm-workspace-test 2. run [code block] 3. send a GET request to `localhost:3000/api/hello` Expected behavior The api runs as expected Prisma information [code block] Environment & setup - OS: Windows 11 - Database: PostgreSQL - Node.js version: v16.14.2 Prisma Version [code block]
Error Output
error - Error: ENOENT: no such file or directory, open 'C:\Users\sbere\Desktop\Projects\prisma-pnpm-workspace-test\apps\frontend\.next\server\pages\api\schema.prisma'
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Fix ENOENT Error for schema.prisma in Next.js API Route
The error occurs because the Next.js build process is unable to locate the 'schema.prisma' file when the PrismaClient is instantiated in the API route. This typically happens in a monorepo setup where the path to the Prisma schema is not correctly referenced due to the directory structure.
Awaiting Verification
Be the first to verify this fix
- 1
Ensure schema.prisma is in the correct location
Verify that the 'schema.prisma' file is located in the expected directory within the package that exports the PrismaClient. The path should be relative to where the Next.js application is running.
bashls apps/backend/prisma/schema.prisma - 2
Update PrismaClient import path
In your Next.js API route, ensure that you are importing the PrismaClient from the correct package and that it is configured to point to the right schema path. Modify the import statement if necessary.
typescriptimport { PrismaClient } from '@your-monorepo/backend'; - 3
Configure PrismaClient to use the correct schema path
If the schema.prisma file is not in the default location, you can specify the path in your PrismaClient initialization. Update your PrismaClient instantiation to include the schema path.
typescriptconst prisma = new PrismaClient({ datasources: { db: { url: 'postgresql://...' } } }); - 4
Rebuild the Next.js application
After making the changes, rebuild your Next.js application to ensure that all changes are applied. This can help in resolving any caching issues that may lead to the ENOENT error.
bashnpm run build - 5
Run the application and test the API
Start your Next.js application and send a GET request to the API route to confirm that the issue is resolved and the API responds as expected.
bashnpm run start
Validation
To confirm the fix worked, send a GET request to 'localhost:3000/api/hello' and ensure that the API responds with the expected output without any errors. Additionally, check the console for any remaining errors related to Prisma.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep