create-t3-turbo: Prisma broken for next when generating prisma client in non-node_modules dirs
My bad 😦
Broke by my PR here: https://github.com/t3-oss/create-t3-turbo/pull/37
Error here, unable to find schema:
https://twitter.com/t3dotgg/status/1582936894991609857

Tried solution here: https://github.com/prisma/prisma/issues/10433#issuecomment-1264687986
Gets further, but new error:
@acme/nextjs:dev: prisma:query SELECT `main`.`Post`.`id`, `main`.`Post`.`title`, `main`.`Post`.`content` FROM `main`.`Post` WHERE 1=1 LIMIT ? OFFSET ?
@acme/nextjs:dev: prisma:query SELECT 1
@acme/nextjs:dev: prisma:query SELECT name FROM sqlite_schema
@acme/nextjs:dev: WHERE type='table'
@acme/nextjs:dev: ORDER BY name;
@acme/nextjs:dev:
@acme/nextjs:dev: failed to fetch, PrismaClientKnownRequestError:
@acme/nextjs:dev: Invalid `prisma.post.findMany()` invocation:
@acme/nextjs:dev:
@acme/nextjs:dev:
@acme/nextjs:dev: The table `main.Post` does not exist in the current database.
@acme/nextjs:dev: at RequestHandler.handleRequestError (webpack-internal:///(api)/../../packages/db/.generated/client/runtime/index.js:31874:19)
@acme/nextjs:dev: at RequestHandler.request (webpack-internal:///(api)/../../packages/db/.generated/client/runtime/index.js:31853:18)
@acme/nextjs:dev: at async PrismaClient._request (webpack-internal:///(api)/../../packages/db/.generated/client/runtime/index.js:32853:24)
@acme/nextjs:dev: at async eval (webpack-internal:///(api)/../../packages/api/src/router/post.ts:19:26)
@acme/nextjs:dev: at async resolveMiddleware (file:///Users/theobrowne/Code/contrib/npm-turbo-t3/node_modules/@trpc/server/dist/router-28a2c129.mjs:190:30)
@acme/nextjs:dev: at async callRecursive (file:///Users/theobrowne/Code/contrib/npm-turbo-t3/node_modules/@trpc/server/dist/router-28a2c129.mjs:226:32)
@acme/nextjs:dev: at async resolve (file:///Users/theobrowne/Code/contrib/npm-turbo-t3/node_modules/@trpc/server/dist/router-28a2c129.mjs:254:24)
@acme/nextjs:dev: at async file:///Users/theobrowne/Code/contrib/npm-turbo-t3/node_modules/@trpc/server/dist/resolveHTTPResponse-f2b378b6.mjs:374:32
@acme/nextjs:dev: at async Promise.all (index 0)
@acme/nextjs:dev: at async resolveHTTPResponse (file:///Users/theobrowne/Code/contrib/npm-turbo-t3/node_modules/@trpc/server/dist/resolveHTTPResponse-f2b378b6.mjs:371:28) {
@acme/nextjs:dev: code: 'P2021',
@acme/nextjs:dev: clientVersion: '4.5.0',
@acme/nextjs:dev: meta: { table: 'main.Post' }
@acme/nextjs:dev: }
assumption is that it can’t actually find the db in next, which is weird because it can totally find it in prisma studio? idk.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 27 (10 by maintainers)
Commits related to this issue
- build(database): specify turbo output directory Seeing this issue that ran into similar issues to us, so I want to see if the fix they tried will make any difference for us. https://github.com/t3-oss... — committed to skillrecordings/products by jbranchaud 2 years ago
- IN-692 applied fixes per https://github.com/t3-oss/create-t3-turbo/issues/42 — committed to weareinreach/InReach by JoeKarow 2 years ago
- IN-692 change prisma client generation location \#comment The only thing in the browser console logs was complaining about getting a bad response from useSession(). Server logs showed next-auth was ha... — committed to weareinreach/InReach by JoeKarow 2 years ago
- IN-692 change prisma client generation location \#comment The only thing in the browser console logs was complaining about getting a bad response from useSession(). Server logs showed next-auth was ha... — committed to weareinreach/InReach by JoeKarow 2 years ago
Using the changes from @subaanqasim got things running for me (very appreciated) locally.
When I run the deploy, I see this:
However, the API calls return:
Seems like either the db package is looking in the wrong place, or the generation output is going maybe two levels up from the root and out of the project?
Anyone hit on anything like this?
Yeah, fair enough - I figured it’s worth mentioning here in case someone else is in a similar situation.
Hey gang - coming back to this issue since I’ve been wrestling it for a few days here. I’m building an app with create-t3-turbo, and have been trying to keep my changes roughly in sync with what’s happened in this repo since I started with it in December.
I was getting
ENOENTerrors from my nextjs project when trying to import prisma – and found my (current) fix in this comment - https://github.com/prisma/prisma/issues/12921#issuecomment-1414839304 - the prisma team have an experimental webpack plugin which seems to address this issue. The prisma issue was linked earlier here, but there’s been some activity since then. Figured a bump might help.oh yeah lol, I got a ENOENT no such file error on the trpc API file pointing towards the prisma schema.
I spent a full day debugging this issue a few days ago, so I thought I’d give my two cents into how I’ve solved it (for now at least, since it’s kinda hacky). Apologies for the long post, but I thought I’d explain my thought processes in case someone with a bigger brain can produce a legit solution based off the information below.
TLDR:
ENOENTerror:turbo.json:Context: the repo I’m working with is essentially a create-t3-turbo monorepo - 2 next apps (with next-auth, but no expo), with prisma and tRPC as shared packages.
From my understanding, the
ENOENTin the nextjs app is due to it trying to import the prisma client from thenode_modulesat the root of the monorepo. Addingpublic-hoist-pattern[]=*prisma*to .npmrc seems to fix that issue by seemingly hoisting the prisma client generation to the root (from https://github.com/prisma/prisma/issues/12921#issuecomment-1152901509). HOWEVER, this will only work ifPrismaClientis imported from@prisma/clientindb/index.tsand not from a custom generator path.However, this then results in an issue for the tRPC api package - the imported prisma client in
api/src/context.tsfrom will be of typeany. This is despite instances of prisma imported from@acme/dbin the next app resolving the types properly (currently, you’ll notice that prisma is typeanyin the next app and api package in create-t3-turbo due to no hoisting). If you set a custom generator path for your prisma schema and create your client from that indb/index.tsthen the prisma type issues fix, but you get theENOENTerror again in the next app due to the custom path of the client not being hoisted up to the root node_modules. Humongous pain.Importing from
@prisma/clientjust exports the default schema generation path (node_modules/.prisma/client). In theory, according to this explanation of how pnpm and prisma client imports works, we should be able to generate the client into the root by havingoutput = "../../../node_modules/.prisma/client"in schema.prisma and import PrismaClient from@prisma/clientindb/index.ts. This is because we’re now hoisting prisma to root and generating the client in the root, so all the prisma stuff is sitting together.Then, to ensure proper caching of the prisma client, change the
db-generateoutput inturbo.jsonto the root:This is what ultimately worked for me - no ENOENT errors, proper type inference of the prisma client in all packages and apps, and proper caching by turborepo.
Also, with this solution, it might be worth locking pnpm version to <7.13.5 in Vercel builds using the
ENABLE_EXPERIMENTAL_COREPACK=1env variable due to this potential issue.As far as I know the ENOENT errors were fixed in this repo already, so not really sure what the issue is in your case man.
Need to sleep and come back to take a look, but these are the first set of changes that let me run dev and get through a clean build with no ENOENT issues and prisma type inference carrying through (including adding and testing a new model in prisma.schema).
/.env
ENABLE_EXPERIMENTAL_COREPACK=1/next.config.mjs:
/.npmrc (I removed
node-linker=hoistedandpublic-hoist-patter[]=*prisma*/turbo.json:
/packages/db/prisma/schema.prisma
/packages/db/.env
Everything else matches current ‘create-t3-turbo-auth’, including a .env matching the example in /apps/nextjs. Can do a better job referencing later.