I am attempting to set up a Node.js Express application using Prisma ORM v7.0.1 and a MongoDB database. I keep running into an Error: @prisma/client did not initialize yet. Please run "prisma generate" and try to import it again. whenever I run npm run dev.
The issue persists even after making several adjustments to my configuration files and schema.
What I have tried
Fixed
schema.prismasyntax: I corrected a missing double quote in the@map("_id")attribute that was causingprisma generateto fail initially.Moved Database URL: I moved the
urlfromschema.prismato myprisma.config.tsfile as required by Prisma v7.Rerunning
prisma generate: I have runnpx prisma generatemultiple times after these changes. The command now runs without validation errors and successfully writes files, but the application error persists.Noticed generated files are TypeScript: The client files are being generated in the specified output directory as TypeScript files (e.g.,
.ts), while my application entry point is a.jsfile using ES Modules.
It seems my prisma generate is now working, but Node/Nodemon still can't initialize the client when running the application.
How do I correctly import the generated Prisma Client given my custom output path and the fact that my source code is JavaScript while the generated files are TypeScript?
My schema.prisma
I have a custom output path for the generated client, and I've tried to fix the MongoDB url issue related to Prisma 7 changes.
generator client {
provider = "prisma-client"
output = "../generated/prisma"
}
datasource db {
provider = "mongodb"
// url = env("DATABASE_URL") // Removed this line due to v7 changes
}
model Region {
id String @id @default(auto()) @map("_id") @db.ObjectId // Corrected the syntax error "_id)"
name String @unique
// ... other fields
}
My index.js (using ESM imports)
My main application file is index.js, and I am using import statements.
import e from "express";
import { config } from "dotenv";
import { PrismaClient } from "@prisma/client/extension"; // This is the line where the error points
config()
const prisma = new PrismaClient()
// ... rest of express app set