question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with running Prisma Client in a Nextjs Application

See original GitHub issue

Bug description

I’m writing a Nextjs application which uses Prisma to generate and communicate to a database. I am running into the following error:

PrismaClientInitializationError2 [PrismaClientInitializationError]:
Invalid `prisma.post.findMany()` invocation:


  Query engine exited with code 101

thread 'main' panicked at 'Could not open datamodel file "E:\\joao\\code-web\\next-firebase-auth\\.next\\server\\pages\\schema.prisma"', query-engine\query-engine\src\opt.rs:184:53
stack backtrace:
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
    at cb (E:\joao\code-web\next-firebase-auth\node_modules\@prisma\client\runtime\index.js:35104:17)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async getServerSideProps (E:\joao\code-web\next-firebase-auth\.next\server\pages\index.js:483:17)
    at async renderToHTML (E:\joao\code-web\next-firebase-auth\node_modules\next\dist\next-server\server\render.js:40:215)
    at async E:\joao\code-web\next-firebase-auth\node_modules\next\dist\next-server\server\next-server.js:111:97
    at async E:\joao\code-web\next-firebase-auth\node_modules\next\dist\next-server\server\next-server.js:104:142
    at async DevServer.renderToHTMLWithComponents (E:\joao\code-web\next-firebase-auth\node_modules\next\dist\next-server\server\next-server.js:136:387)
    at async DevServer.renderToHTML (E:\joao\code-web\next-firebase-auth\node_modules\next\dist\next-server\server\next-server.js:137:610)
    at async DevServer.renderToHTML (E:\joao\code-web\next-firebase-auth\node_modules\next\dist\server\next-dev-server.js:36:578) {
  clientVersion: '2.22.1',
  errorCode: undefined
}

whenever I try to run the app with next dev. I have tried generating the Prisma client in a dedicated file, and importing where needed, generating the client in the api routes, and generating the client in the getServerSideProps method in a page route. they all returned the same error (with a different location for the datamodel file that could not be opened)

  • I am aware prisma has some issues with yarn2, however I am using npm
  • the database is being generated, as I am able to interact with it through Prisma Studio

How to reproduce

  1. create a next app npx create-next-app .
  2. configure prisma and prisma client
  3. try instantiating the prisma client and performing queries

Expected behavior

Expected behaviour was for the app not to crash

Prisma information

schema:

datasource db {
  provider = "sqlite"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}

model User {
  uid      String   @id
  email    String?  @unique
  name     String?
  provider String?
  photoUrl String?
  posts    Post[]
  comments Comment[]
}

model Post {
  id        String    @id @default(cuid())
  author    User      @relation(fields: [authorId], references: [uid])
  authorId  String
  title     String
  content   String
  createdAt DateTime  @default(now())
  comments  Comment[]
}

model Comment {
  id       String @id @default(cuid())
  author   User   @relation(fields: [authorId], references: [uid])
  authorId String
  content  String
  post     Post   @relation(fields: [postId], references: [id])
  postId   String
}

Environment & setup

  • OS: Windows 8.1
  • Database: SQLite
  • Node.js version: 14.15.4
  • Prisma version:
prisma               : 2.22.1
@prisma/client       : 2.22.1
Current platform     : windows
Query Engine         : query-engine 60cc71d884972ab4e897f0277c4b84383dddaf6c (at node_modules\@prisma\engines\query-engine-windows.exe)
Migration Engine     : migration-engine-cli 60cc71d884972ab4e897f0277c4b84383dddaf6c (at node_modules\@prisma\engines\migration-engine-windows.exe)
Introspection Engine : introspection-core 60cc71d884972ab4e897f0277c4b84383dddaf6c (at node_modules\@prisma\engines\introspection-engine-windows.exe)
Format Binary        : prisma-fmt 60cc71d884972ab4e897f0277c4b84383dddaf6c (at node_modules\@prisma\engines\prisma-fmt-windows.exe)
Default Engines Hash : 60cc71d884972ab4e897f0277c4b84383dddaf6c
Studio               : 0.379.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
pantharshit00commented, May 27, 2021

@jvcmarcenes You are importing the client from wrong location which messes up the webpack annotation we have.

Please change the prisma/index.ts file to the following:

import { PrismaClient, Prisma } from "@prisma/client";

async function main() {
  const prisma = new PrismaClient({
    log: ["query"],
  });

  const ids = [1, "2);SELECT 1;SELECT (1", 3];
  const garages = await prisma.$queryRaw(
    `SELECT grg.id FROM public.garage as grg where grg.id in (${ids})`
  );
  console.log(garages);
  prisma.$disconnect();
}

main();

After this change, I was able to run the app: image

I am going to close this.

1reaction
jvcmarcenescommented, May 18, 2021

Can someone maybe easily create a simple minimal reproduction project using Webpack 5 that shows this crash? Would make our investigation and reproduction much faster.

here it is: jvcmarcenes/prisma-nextjs-webpack5

the index page, ‘/’, will call getServerSideProps, to fetch data from the endpoint ‘/api/comments’ the endpoint handler will make a query to the PrismaClient, instantiated at prisma/index.ts the application works if the api endpoint doesn’t make a Prisma query

Read more comments on GitHub >

github_iconTop Results From Across the Web

Best practice for instantiating PrismaClient with Next.js
The solution in this case is to instantiate a single instance PrismaClient and save it on the global object. Then we keep a...
Read more >
How to fix the `Already 10 Prisma Clients are actively running ...
I was using Prisma in my Next.js app and I was doing it wrong. I was initializing a new PrismaClient object in every...
Read more >
next.js ENOENT trying to open schema.prisma #12921 - GitHub
Bug description In a monorepo, when exporting PrismaClient from one package and consuming it in a next.js api route, I get error -...
Read more >
How to use Prisma in a Next.js app - Daily.dev
In this post, we will learn how to use Prisma, an ORM for JavaScript and TypeScript, in a Next.js app.
Read more >
How to Build a Fullstack App with Next.js, Prisma, and ... - Vercel
Create a new API route to delete a post. Update the API route to modify the database using the Prisma Client. This code...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found