Next.js 13 - ENOENT: no such file or directory, open /var/task/.next/server/chunks/schema.prisma
shadcn opened this issue · comments
Bug description
When deploying a Next.js 13 (with app dir) on Vercel, I'm seeing the following error:
Error: ENOENT: no such file or directory, open '/var/task/.next/server/chunks/schema.prisma'
at Object.openSync (node:fs:585:3)
at Object.readFileSync (node:fs:453:35)
at new LibraryEngine (/var/task/.next/server/chunks/538.js:28292:45)
at PrismaClient.getEngine (/var/task/.next/server/chunks/538.js:32706:24)
at new PrismaClient (/var/task/.next/server/chunks/538.js:32678:37)
at Object.4963 (/var/task/.next/server/app/page.js:374:14)
at __webpack_require__ (/var/task/.next/server/webpack-runtime.js:25:42)
at page (/var/task/.next/server/app/page.js:237:87)
at createComponentTree (/var/task/node_modules/next/dist/server/app-render.js:578:80)
at /var/task/node_modules/next/dist/server/app-render.js:663:62 {
errno: -2,
syscall: 'open',
code: 'ENOENT',
path: '/var/task/.next/server/chunks/schema.prisma',
clientVersion: '4.5.0',
page: '/'
}
RequestId: 9ce84669-6d69-44b5-a6fc-f6e572a6a845 Error: Runtime exited with error: exit status 1
Runtime.ExitError
I've tried a few apps to try and reproduce this and this only happens when you make the page dynamic.
A few ways I've been able to reproduce this:
- Using the
export const revalidate = 0
segment config on a page - Calling
headers()
from "next/headers" inside the page.
✅ This works:
import { db } from "@/lib/db"
export default async function Page() {
const messages = await db.message.findMany()
return ...
}
❌ This does not work:
import { db } from "@/lib/db"
export const revalidate = 0 // 👈 Adding this results in the error above.
export default async function Page() {
const messages = await db.message.findMany()
return ...
}
❌ This does not work:
import { headers } from "next/headers"
import { db } from "@/lib/db"
export default async function Page() {
headers() // 👈 Adding this results in the error above.
const messages = await db.message.findMany()
return ...
}
How to reproduce
I have a sample app here: https://github.com/shadcn/next-prisma-app
- Create a new Next.js 13 app with a prisma model.
- Create a new file at
app/page.tsx
and try calling prisma there. - Add
export const revalidate = 0
- Deploy to Vercel and try accessing the page at
/
- You should the error above.
- Remove the export and try again.
- Error should be gone.
Expected behavior
Expected prisma to work with the segment config.
Prisma information
generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialIntegrity"]
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
referentialIntegrity = "prisma"
}
model Message {
id Int @id @default(autoincrement())
content String?
}
Environment & setup
- OS: Vercel (works fine locally on MacOS)
- Database: MySQL
- Node.js version: 16
Prisma Version
prisma : 4.5.0
@prisma/client : 4.5.0
Current platform : darwin
Query Engine (Node-API) : libquery-engine 0362da9eebca54d94c8ef5edd3b2e90af99ba452 (at node_modules/@prisma/engines/libquery_engine-darwin.dylib.node)
Migration Engine : migration-engine-cli 0362da9eebca54d94c8ef5edd3b2e90af99ba452 (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core 0362da9eebca54d94c8ef5edd3b2e90af99ba452 (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary : prisma-fmt 0362da9eebca54d94c8ef5edd3b2e90af99ba452 (at node_modules/@prisma/engines/prisma-fmt-darwin)
Format Wasm : @prisma/prisma-fmt-wasm 4.5.0-43.0362da9eebca54d94c8ef5edd3b2e90af99ba452
Default Engines Hash : 0362da9eebca54d94c8ef5edd3b2e90af99ba452
Studio : 0.476.0
Preview Features : referentialIntegrity
Notes:
- the Next.js config has an experimental setting, which is not documented in Next.js docs at the moment.
https://github.com/shadcn/next-prisma-app/blob/main/next.config.js
experimental: {
appDir: true,
},
revalidate
is documented differently in https://nextjs.org/docs/basic-features/data-fetching/incremental-static-regeneration (probably because this new way to set it was added in Next.js 13?)
It enables ISR:Next.js allows you to create or update static pages after you’ve built your site. Incremental Static Regeneration (ISR) enables you to use static-generation on a per-page basis, without needing to rebuild the entire site. With ISR, you can retain the benefits of static while scaling to millions of pages.
I meant this new segment config in the beta docs: https://beta.nextjs.org/docs/api-reference/segment-config#revalidate
Thanks for the link!
I have exactly the same problem with a monorepo structure using yarn and Next13 with the new AppDir feature enabled.
I've updated the issue description with more details. I found out this happens when you opt into dynamic pages. Example by using a segment config or using headers()
or cookies()
.
I've temporarily generated the prisma-client to src dir, bypassing this problem
generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialIntegrity", "interactiveTransactions"]
output = "../shared/prisma-client"
}
db.ts
import { PrismaClient } from "@/shared/prisma-client";
@FlatMapIO It worked. THANK YOU 🙌
I've temporarily generated the prisma-client to src dir, bypassing this problem
For me this workaround brings another error which it searches for schema file inside default node_modules
dir.
Error: ENOENT: no such file or directory, open '\project\node_modules\prisma\prisma-client\schema.prisma'
generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialIntegrity", "interactiveTransactions"]
output = "../shared/prisma-client"
}
I have observed similar issue
If you're using nextjs, try adding prisma to the serverComponentsExternalPackages
in your config:
// next.config.js
const nextConfig = {
experimental: {
appDir: true,
serverComponentsExternalPackages: ['@prisma/client'],
},
};
https://beta.nextjs.org/docs/api-reference/next.config.js#servercomponentsexternalpackages
^ Prisma is now added to the default list, I've updated the docs there to reflect that.
^ Prisma is now added to the default list, I've updated the docs there to reflect that.
@leerob I have solved the same issue by adding serverComponentsExternalPackages: ['@prisma/client']
to next.config.js with next@13.0.2
.
I don't have prisma
install on my project, only @prisma/client
.
I'm also experiencing the same error, I have a monorepo and prisma lives in a different package than the nextjs app.
None of the suggestions above work for me.
@simonepizzamiglio could you open a new issue and provide information to get to a reproduction?
^ Prisma is now added to the default list, I've updated the docs there to reflect that.
https://github.dev/nozich/nextprisma
13.0.2 working as default in dev, build and start
But not working with "dev --turbo"
I think this issue can be closed as it is resolved by adding serverComponentsExternalPackages: ['@prisma/client']
into the next config. (#16117 (comment))
@nozich this is separate from Turbopack support, that would be a different thread to discuss. Turbopack is in alpha.
I can confirm the reproduction that @shadcn shared originally (13.0.2 a monorepo, app dir, no custom output
in schema.prisma
), and I can also reproduce that it has been fixed with next@13.0.2 as @leerob shared. Thanks!
For everyone who commented here but is actually using a monorepo or a custom output
config in schema.prisma
, we have quite a few issues for your case. Some examples: #12853 #12921 #12823 Please subscribe to these for updates (which we fortunately will be able to post soon 🔜).
I made the next.config.js thing and nothing this is the error
- error ./node_modules/@mapbox/node-pre-gyp/lib/clean.js:8:0
Module not found: Can't resolve 'fs'
https://nextjs.org/docs/messages/module-not-found
Import trace for requested module:
./node_modules/@mapbox/node-pre-gyp/lib/ sync ^./.*$
./node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js
./node_modules/bcrypt/bcrypt.js
./src/app/api/auth/[...nextauth]/route.ts
./src/components/SideNav.tsx
./src/components/index.ts
./src/components/NewTweetForm.tsx
- wait compiling...
- warn Fast Refresh had to perform a full reload due to a runtime error.
- error ./node_modules/@mapbox/node-pre-gyp/lib/clean.js:8:0
Module not found: Can't resolve 'fs'
https://nextjs.org/docs/messages/module-not-found
Import trace for requested module:
./node_modules/@mapbox/node-pre-gyp/lib/ sync ^./.*$
./node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js
./node_modules/bcrypt/bcrypt.js
./src/app/api/auth/[...nextauth]/route.ts
./src/components/SideNav.tsx
./src/components/index.ts
./src/components/NewTweetForm.tsx
[webpack.cache.PackFileCacheStrategy] Caching failed for pack: Error: ENOENT: no such file or directory, rename '/home/kali/projects/t3/project-1/.next/cache/webpack/client-development-fallback/0.pack.gz_' -> '/home/kali/projects/t3/project-1/.next/cache/webpack/client-development-fallback/0.pack.gz'
^
This is my config file:
/** @type {import('next').NextConfig} */
const nextConfig = {
images: {
domains: ["lh3.googleusercontent.com"],
},
experimental: {
appDir: true,
serverComponentsExternalPackages: ["bcrypt", "@prisma/client"],
},
};
module.exports = nextConfig;