P
Prisma3mo ago
simo

TypeError: Cannot read properties of undefined (reading 'exec')

"Hi, does anyone know why I'm getting a TypeError: Cannot read properties of undefined (reading 'exec') when using Prisma with @prisma/adapter-neon and Next.js? The error seems to originate in library.js and could be related to the environment or connection configuration. I'm attaching a screenshot with the stack trace and debug info. Thanks in advance!"
No description
13 Replies
RaphaelEtim
RaphaelEtim3mo ago
Hi @simo Can you please show how you are instantiating and using Prisma Client in your code?
simo
simoOP3mo ago
// prisma/schema.prisma


generator client {
provider = "prisma-client-js"
previewFeatures = ["driverAdapters"]
binaryTargets = ["native"]
}

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

//client.ts

import ws from "ws"
import { Pool, neonConfig } from "@neondatabase/serverless"
import { PrismaNeon } from "@prisma/adapter-neon"
import { PrismaClient } from "@prisma/client"

const prismaClientSingleton = () => {
neonConfig.webSocketConstructor = ws
neonConfig.poolQueryViaFetch = true
const pool = new Pool({ connectionString: `${process.env.DATABASE_URL}` })
const adapter = new PrismaNeon(pool)
const db = new PrismaClient({ adapter })

return db
}

declare const globalThis: {
prismaGlobal: ReturnType<typeof prismaClientSingleton>
} & typeof global
const db = globalThis.prismaGlobal ?? prismaClientSingleton()
export default db
if (process.env.NODE_ENV !== "production") globalThis.prismaGlobal = db
// prisma/schema.prisma


generator client {
provider = "prisma-client-js"
previewFeatures = ["driverAdapters"]
binaryTargets = ["native"]
}

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

//client.ts

import ws from "ws"
import { Pool, neonConfig } from "@neondatabase/serverless"
import { PrismaNeon } from "@prisma/adapter-neon"
import { PrismaClient } from "@prisma/client"

const prismaClientSingleton = () => {
neonConfig.webSocketConstructor = ws
neonConfig.poolQueryViaFetch = true
const pool = new Pool({ connectionString: `${process.env.DATABASE_URL}` })
const adapter = new PrismaNeon(pool)
const db = new PrismaClient({ adapter })

return db
}

declare const globalThis: {
prismaGlobal: ReturnType<typeof prismaClientSingleton>
} & typeof global
const db = globalThis.prismaGlobal ?? prismaClientSingleton()
export default db
if (process.env.NODE_ENV !== "production") globalThis.prismaGlobal = db
RaphaelEtim
RaphaelEtim3mo ago
In your client.ts file, I think this is the declaration you need
import { Pool } from "@neondatabase/serverless";
import { PrismaNeon } from "@prisma/adapter-neon";
import { PrismaClient } from "@prisma/client";

export function createPrismaClient() {
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
const adapter = new PrismaNeon(pool);
const prisma = new PrismaClient({ adapter: adapter });

return prisma;
}
import { Pool } from "@neondatabase/serverless";
import { PrismaNeon } from "@prisma/adapter-neon";
import { PrismaClient } from "@prisma/client";

export function createPrismaClient() {
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
const adapter = new PrismaNeon(pool);
const prisma = new PrismaClient({ adapter: adapter });

return prisma;
}
You can now import it like this
import { createPrismaClient } from "./client";
import { createPrismaClient } from "./client";
simo
simoOP3mo ago
Thank you very much for your help. I have made the suggested changes and I still have the same error. I suspect that it may be something wrong with the version of openSSL I have installed on my Windows OS. Or maybe I have openSSL configured wrong What version of openssl do you recommend?
RaphaelEtim
RaphaelEtim3mo ago
Can you please confirm if this will be deployed to the edge?
simo
simoOP3mo ago
No, it is not deployed to edge Sorry, I don't have access to the code right now, but when I can, I'll attach all the possible parts of the code.
RaphaelEtim
RaphaelEtim3mo ago
In that case, you can simply make do the code below in your client.ts filewith
import { PrismaClient } from '@prisma/client'

const globalForPrisma = global as unknown as { prisma: PrismaClient }

export const prisma = globalForPrisma.prisma || new PrismaClient()

if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma

export default prisma
import { PrismaClient } from '@prisma/client'

const globalForPrisma = global as unknown as { prisma: PrismaClient }

export const prisma = globalForPrisma.prisma || new PrismaClient()

if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma

export default prisma
simo
simoOP3mo ago
I still have the same error
RaphaelEtim
RaphaelEtim3mo ago
Is it possible to share the repo so i can take a look
simo
simoOP3mo ago
I have cleared the npm cache and reinstalled node.js
RaphaelEtim
RaphaelEtim3mo ago
After clearing the npm cache and reinstalling node, does the error persist? if yes, is it possible to share a reproducible repo with me
simo
simoOP3mo ago
Yes, the error persists. I'll share a repo with you this afternoon. Thanks
RaphaelEtim
RaphaelEtim3mo ago
Looking forward to it. Thanks
Want results from more Discord servers?
Add your server