PrismaClientInitializationError

Hello! I'm new to posting here. I'm developing the Next.js app and deploying it to AWS Amplify. However, I'm having problems using Prisma and Auth.js. When I use both prima and auth's session, I get a "PrismaClientInitializationError." The local and Vercel hosting cases are working without this issue. Do you have any thoughts?
No description
12 Replies
Prisma AI Help
Prisma AI Helpā€¢4w ago
You opted for speed from this bot. Let's conjure up an answer right now! Based on the knowledge sources provided, it appears that you're encountering a common issue when deploying a Next.js application with Prisma and Auth.js to AWS Amplify. This problem is specifically related to the initialization of the Prisma Client in the AWS Amplify environment. Let me provide some insights and potential solutions: 1. The error you're seeing, "PrismaClientInitializationError," is often caused by Prisma not being able to find the schema.prisma file in the deployed environment. This is a known issue when deploying to AWS Amplify, as discussed in this GitHub issue. 2. The root cause seems to be that AWS Amplify deploys API routes to Lambda functions, where the schema.prisma file is not present in the expected location. 3. A potential solution that has worked for some users is to modify your next.config.js file. You can try adding the following webpack configuration:
webpack: (config, { defaultLoaders, isServer }) => {
if (isServer) {
config.externals.push('_http_common');
}
return config;
},
webpack: (config, { defaultLoaders, isServer }) => {
if (isServer) {
config.externals.push('_http_common');
}
return config;
},
4. Additionally, you might need to ensure that your schema.prisma file is properly copied to the deployment environment. However, the exact method to do this in AWS Amplify is not clear from the provided knowledge sources. 5. It's worth noting that this issue doesn't occur on Vercel or in local development, which suggests it's specific to the AWS Amplify deployment process. 6. Some users have reported success by using the experimental-serverless-trace target in their Next.js configuration, as mentioned in this comment. You could try adding this to your next.config.js:
target: 'experimental-serverless-trace'
target: 'experimental-serverless-trace'
Given the complexity of this issue and the lack of a definitive solution in the provided knowledge sources, you might want to consider the following steps: 1. Try implementing the suggested cha nges to your next.config.js file. 2. Ensure that your Prisma schema and client are properly generated during the build process on AWS Amplify. 3. Double-check that your environment variables, especially the database connection string, are correctly set in the AWS Amplify environment. If these steps don't resolve the issue, you may need to reach out to AWS Amplify support or consider alternative deployment options that are known to work well with Prisma and Auth.js, such as Vercel. šŸ¤” Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly. In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.
Mildseven-Light
Mildseven-LightOPā€¢4w ago
This issue only happens using Auth.js's session.
export default async function Dashboard() {
const session = await auth()
console.log(session)

const prisma = new PrismaClient()
if (!session) {
return
redirect('/')
}

const products = await prisma.product.findMany()
return (
<main>
<h1>This is Dashboard</h1>
<ul>
{products.map((product) => (
<li key={product.id}>
{product.name} - {product.price}
</li>
))}
</ul>
</main >
);
}
export default async function Dashboard() {
const session = await auth()
console.log(session)

const prisma = new PrismaClient()
if (!session) {
return
redirect('/')
}

const products = await prisma.product.findMany()
return (
<main>
<h1>This is Dashboard</h1>
<ul>
{products.map((product) => (
<li key={product.id}>
{product.name} - {product.price}
</li>
))}
</ul>
</main >
);
}
Sorry, I'm new to using Discord. I'm not sure how to form the code.šŸ˜©
RaphaelEtim
RaphaelEtimā€¢4w ago
Hi @Mildseven-Light Does this happen only on deploy to AWS Amplify? Can you log the output of auth() to ensure it retrieves the session correctly in AWS Amplify's environment.
Mildseven-Light
Mildseven-LightOPā€¢4w ago
I deployed the same app to Vercel, and it works fine.
Mildseven-Light
Mildseven-LightOPā€¢4w ago
Also, the output of auth() is working correctly.
No description
RaphaelEtim
RaphaelEtimā€¢4w ago
From the error message, i can see that it says
Prisma Client could not locate the Query Engine for runtime "rhel-openssl-1.0.x".

This happened because Prisma Client was generated for "rhel-openssl-3.0.x", but the actual deployment requ...

Add "rhel-openssl-1.O.x" to 'binaryTargets' in the "schema prisma" file and run 'prisma generate after sa...
Prisma Client could not locate the Query Engine for runtime "rhel-openssl-1.0.x".

This happened because Prisma Client was generated for "rhel-openssl-3.0.x", but the actual deployment requ...

Add "rhel-openssl-1.O.x" to 'binaryTargets' in the "schema prisma" file and run 'prisma generate after sa...
Can you add rhel-openssl-1.O.x to 'binaryTargets' in the schema prisma file
Mildseven-Light
Mildseven-LightOPā€¢4w ago
Do you know how to add rhel-openssl-1.O.x?
RaphaelEtim
RaphaelEtimā€¢4w ago
In your schema.prisma file, you should see this section
generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "rhel-openssl-1.0.x"]
}
generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "rhel-openssl-1.0.x"]
}
Mildseven-Light
Mildseven-LightOPā€¢4w ago
I'm deploying right now. Thank you for your help!
RaphaelEtim
RaphaelEtimā€¢4w ago
You're welocme. Let me know if you run into any more issues
Mildseven-Light
Mildseven-LightOPā€¢4w ago
Yes will do! @RaphaelEtim Thank you so much for your help! It is working now! You made my day!
RaphaelEtim
RaphaelEtimā€¢4w ago
You're welcome

Did you find this page helpful?