Next.js Discord

Discord Forum

Prisma is not initiated

Answered
Persian posted this in #help-forum
Open in Discord
PersianOP
versions:
"next": "^15.5.6",
"next-auth": "^4.24.13",
DB: Supabase
ORM: Prisma

I am following every example, but no matter what I do I get:
⚠️[CB signup]google Sign-in Error: Error: @prisma/client did not initialize yet. Please run "prisma generate" and try to import it again.

This is my prisma.ts:
// lib/prisma.ts
// current solution
import { PrismaClient } from "@prisma/client";

declare global {
  var prisma: PrismaClient | undefined;
}

export function getPrisma(): PrismaClient {
  if (!global.prisma) {
    global.prisma = new PrismaClient({
      log: ["query"], // valfritt
    });
  }
  return global.prisma;
}

It has also been written like this:
import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient()
export prisma;

The problem I get is in my repository functions.
const handler = NextAuth({
...providers,
callbacks:{
signIn:(args){
//codeUserService.create({
            email: user.email,
            name: user.name,
            image: user.image,
          });
}
}
})

UserService calls UserRepositories which can look like this:
export async function create(data: IUserCreate): Promise<IUser> {
  const prisma = getPrisma();
  const newUser = await prisma.user.create({
    data,
  });
  return newUser;
}

I have never seen this kund of solution before:
const prisma = getPrisma();

Normaly you just import it
import {prisma} from "@/lib/prisma.ts"

But that gives me the same error.

I have tried run
npx prisma generate
multiple times. I have deleted everything and re-installed it.

I have come to an end where I don´t know to keep looking and try...

Ask question if you feel you need more information or if you have any tips, please share.
Answered by Persian
Hey!
You haven´t shared a result so I am guession you haven´t solwed it. I got it to work by doing the following in prisma.schema file.
 provider = "prisma-client-js"

Added "-js"

Config file:
import "dotenv/config";
import { defineConfig, env } from "prisma/config";

export default defineConfig({
  schema: "prisma/schema.prisma",
  migrations: {
    path: "prisma/migrations",
  },
  engine: "classic",
  datasource: {
    url: env("DIRECT_URL"),
  },
});

And my prisma.ts file:
// lib/prisma.ts
import { PrismaClient } from "@prisma/client";
const prismaClientSingelton = () => {
  return new PrismaClient();
};
declare const globalThis: {
  prismaGlobal: ReturnType<typeof prismaClientSingelton>;
} & typeof global;

const prisma = globalThis.prismaGlobal ?? prismaClientSingelton();

export default prisma;

if (process.env.NODE_ENV !== "production") globalThis.prismaGlobal = prisma;


## As I understand it.
The current documentation we can read on multiple sites adds the generated folder in the application within app or src (depending on you config and folder structure).

By changing to
prisma-client-js
The Prisma Client will be generated in node_modules instead when you run:
 npx prisma generate 

I haven´t found anything that says this way is wrong, bad practice now or in the future.
I am running on
 "next": "^15.5.6" 
View full answer

31 Replies

@Persian versions: "next": "^15.5.6", "next-auth": "^4.24.13", DB: Supabase ORM: Prisma I am following every example, but no matter what I do I get: > ⚠️[CB signup]google Sign-in Error: Error: @prisma/client did not initialize yet. Please run "prisma generate" and try to import it again. This is my prisma.ts: js // lib/prisma.ts // current solution import { PrismaClient } from "@prisma/client"; declare global { var prisma: PrismaClient | undefined; } export function getPrisma(): PrismaClient { if (!global.prisma) { global.prisma = new PrismaClient({ log: ["query"], // valfritt }); } return global.prisma; } It has also been written like this: js import { PrismaClient } from "@prisma/client"; const prisma = new PrismaClient() export prisma; The problem I get is in my repository functions. js const handler = NextAuth({ ...providers, callbacks:{ signIn:(args){ //code → UserService.create({ email: user.email, name: user.name, image: user.image, }); } } }) UserService calls UserRepositories which can look like this: js export async function create(data: IUserCreate): Promise<IUser> { const prisma = getPrisma(); const newUser = await prisma.user.create({ data, }); return newUser; } I have never seen this kund of solution before: js const prisma = getPrisma(); Normaly you just import it import {prisma} from "@/lib/prisma.ts" But that gives me the same error. I have tried run npx prisma generate multiple times. I have deleted everything and re-installed it. I have come to an end where I don´t know to keep looking and try... Ask question if you feel you need more information or if you have any tips, please share.
show us your schema.prisma
@strikx show us your schema.prisma
PersianOP
It is long byt I guess you wanna see this:

generator client {
  provider = "prisma-client"
  output   = "../generated/prisma"
}

datasource db {
  provider  = "postgresql"
  url       = env("DATABASE_LOCAL_URL")
  directUrl = env("DIRECT_URL")
}


Ad-on:
I can not import from this folder that is shown in some examples:
"../generated/prisma"

Get ts error that it doesnt exist.
Img of the generated folder:
I can not import from this folder that is shown in some examples:

run prisma generate as usual then restart Typescript server
@strikx delete output line
PersianOP
Do you men import @prisma/client on the schema file?!
@Persian Do you men import @prisma/client on the schema file?!
have you tried restarting the typescript serve after regenerating prisma
@Persian Do you men import @prisma/client on the schema file?!
No, in pisma.ts how you already haf
Had*
@alfonsüs ardani have you tried restarting the typescript serve after regenerating prisma
He has custom path for generated prisma, and dont use it
I see
Nvm
I forgot what i written
@alfonsüs ardani have you tried restarting the typescript serve after regenerating prisma
PersianOP
Yes I have tried that as well.
@strikx He has custom path for generated prisma, and dont use it
PersianOP
I have this:
"baseUrl": ".",
"paths": {
"@/": ["./"]
}

This "import { PrismaClient } from "@prisma/client";" is auto-complete
I don´t get which path I should set it to.. Should it come from the generated folder?!

In Prisma examples they import from the generated folder, but cant destruct anything from that path.
import { PrismaClient } from "@/generated/prisma";


I have also tried this one:
// lib/prisma.ts
import { PrismaClient } from "@prisma/client";
//:camera_with_flash:ByteGrad →  prisma in next.js - my fav way to work with databases
const prismaClientSingelton = () => {
  return new PrismaClient();
};

declare const globalThis: {
  prismaGlobal: ReturnType<typeof prismaClientSingelton>;
} & typeof global;

const prisma = globalThis.prismaGlobal ?? prismaClientSingelton();

export default prisma;

if (process.env.NODE_ENV !== "production") globalThis.prismaGlobal = prisma;

Which in my world should work fine.
If you remove output line from schema, Importing @prisma/client should work.
PersianOP
When i do prisma generate I get this:
  jobworker git:(main) ✗ npx prisma generate
🌴 env Environment: development
Loaded Prisma config from prisma.config.ts.

Prisma config detected, skipping environment variable loading.
Prisma schema loaded from prisma/schema.prisma

✔ Generated Prisma Client (6.18.0) to ./generated/prisma in 72ms

But when I try to import it in:
root/lib/prisma.ts
import { PrismaClient } from "../generated/prisma";

I get this error:
Cannot find module '../generated/prisma' or its corresponding type declarations.ts(2307)

When I do this kind of import:
import { PrismaClient } from "@/generated/prisma";

I get the same error:
Cannot find module '@/generated/prisma' or its corresponding type declarations.ts(2307)

I understand that I am doing something wrong. But I don´t get what. I have followed every example.
@strikx If you remove output line from schema, Importing @prisma/client should work.
PersianOP
Removing output line.. And using this import: @prisma/client in prisma.ts
@strikx If you remove output line from schema, Importing @prisma/client should work.
PersianOP
I did this:

generator client {
  provider = "prisma-client"
}

Ran
npx prisma generate

I got the generated folder in my prisma folder.

Doing this in prisma.ts now:
// lib/prisma.ts
import { PrismaClient } from "@prisma/client"; ← ← ←

const prismaClientSingelton = () => {
  return new PrismaClient();
};

declare const globalThis: {
  prismaGlobal: ReturnType<typeof prismaClientSingelton>;
} & typeof global;

const prisma = globalThis.prismaGlobal ?? prismaClientSingelton();

export default prisma;

if (process.env.NODE_ENV !== "production") globalThis.prismaGlobal = prisma;

Restarting:
npm run dev

Pressing a btn which will use prisma.[table].create(...)

I get this error:
Error: @prisma/client did not initialize yet. Please run "prisma generate" and try to import it again.
    at prismaClientSingelton (lib/prisma.ts:6:10)
    at eval (lib/prisma.ts:13:43)
    at <unknown> (rsc)/./lib/prisma.ts (/Users/oscarthroedsson/Documents/code/myownprojects/jobworker/.next/server/app/api/auth/[...nextauth]/route.js:99:1)
    at eval (webpack-internal:///(rsc)/./lib/repositories/User/UserRepositories.ts:11:69)
    at <unknown> (rsc)/./lib/repositories/User/UserRepositories.ts (/Users/oscarthroedsson/Documents/code/myownprojects/jobworker/.next/server/app/api/auth/[...nextauth]/route.js:132:1)
    at eval (webpack-internal:///(rsc)/./lib/Services/User/UserService.ts:12:97)
    at <unknown> (rsc)/./lib/Services/User/UserService.ts (/Users/oscarthroedsson/Documents/code/myownprojects/jobworker/.next/server/app/api/auth/[...nextauth]/route.js:88:1)
    at eval (webpack-internal:///(rsc)/./app/api/auth/[...nextauth]/route.ts:10:88)
    at <unknown> (rsc)/./app/api/auth/[...nextauth]/route.ts (/Users/oscarthroedsson/Documents/code/myownprojects/jobworker/.next/server/app/api/auth/[...nextauth]/route.js:22:1)
    at __webpack_exec__ (.next/server/app/api/auth/[...nextauth]/route.js:371:39)
    at <unknown> (.next/server/app/api/auth/[...nextauth]/route.js:372:415)
    at <unknown> (.next/server/app/api/auth/[...nextauth]/route.js:372:47)
    at Object.<anonymous> (.next/server/app/api/auth/[...nextauth]/route.js:375:3)
  4 | //📸ByteGrad →  prisma in next.js - my fav way to work with databases
  5 | const prismaClientSingelton = () => {
> 6 |   return new PrismaClient();
    |          ^
  7 | };
  8 |
  9 | declare const globalThis: { {
  page: '/api/auth/_log'
}


Using prisma like this in my file:
import { IUser, IUserCreate } from "@/lib/Interfaces/User/IUser";
import prisma from "@/lib/prisma";

import { userSchema } from "@/lib/Schemas/User";
import z from "zod";

export async function create(data: IUserCreate): Promise<IUser> {
  const newUser = await prisma.user.create({
    data,
  });
  return newUser;
}
hmm, thats weird
@strikx hmm, thats weird
PersianOP
Yep. I downgraded the next.js before when I was using authO. Maybe update it again 🤔

I got the tip to add:
engineType = "client"
In schema file
I am also having the same exact issue. I fixed it temporarily by importing PrismaClient from “../prisma/generated/client”.

Now I have broke it again fails to initialize.
@Persian What next version do you have?
Operating System:
Platform: win32
Arch: x64
Version: Windows 11 Home
Available memory (MB): 32659
Available CPU cores: 20
Binaries:
Node: 20.18.0
npm: 10.9.4
Yarn: 1.22.22
pnpm: N/A
Relevant Packages:
next: 16.0.1 // Latest available version is detected (16.0.1).
eslint-config-next: N/A
react: 19.2.0
react-dom: 19.2.0
typescript: 5.9.3
Next.js Config:
output: N/A
@swiss Operating System: Platform: win32 Arch: x64 Version: Windows 11 Home Available memory (MB): 32659 Available CPU cores: 20 Binaries: Node: 20.18.0 npm: 10.9.4 Yarn: 1.22.22 pnpm: N/A Relevant Packages: next: 16.0.1 // Latest available version is detected (16.0.1). eslint-config-next: N/A react: 19.2.0 react-dom: 19.2.0 typescript: 5.9.3 Next.js Config: output: N/A
PersianOP
Hey!
You haven´t shared a result so I am guession you haven´t solwed it. I got it to work by doing the following in prisma.schema file.
 provider = "prisma-client-js"

Added "-js"

Config file:
import "dotenv/config";
import { defineConfig, env } from "prisma/config";

export default defineConfig({
  schema: "prisma/schema.prisma",
  migrations: {
    path: "prisma/migrations",
  },
  engine: "classic",
  datasource: {
    url: env("DIRECT_URL"),
  },
});

And my prisma.ts file:
// lib/prisma.ts
import { PrismaClient } from "@prisma/client";
const prismaClientSingelton = () => {
  return new PrismaClient();
};
declare const globalThis: {
  prismaGlobal: ReturnType<typeof prismaClientSingelton>;
} & typeof global;

const prisma = globalThis.prismaGlobal ?? prismaClientSingelton();

export default prisma;

if (process.env.NODE_ENV !== "production") globalThis.prismaGlobal = prisma;


## As I understand it.
The current documentation we can read on multiple sites adds the generated folder in the application within app or src (depending on you config and folder structure).

By changing to
prisma-client-js
The Prisma Client will be generated in node_modules instead when you run:
 npx prisma generate 

I haven´t found anything that says this way is wrong, bad practice now or in the future.
I am running on
 "next": "^15.5.6" 
Answer
@swiss Cool, thanks. I’ll give it a try here in a second.
PersianOP
No worries. Update me and tell me how it went 🙂
@Persian No worries. Update me and tell me how it went 🙂
Worked with Next 16 🙂 Thanks a lot
@swiss Worked with Next 16 🙂 Thanks a lot
PersianOP
Yeey! Good!