0
X [ERROR] service core:user:ttm: Uncaught Error: @prisma/client did not initialize yet. Please run "prisma generate" and try to import it again.

    at null.<anonymous> (index.js:1364:15) in PrismaClient
    at null.<anonymous> (index.js:3969:14)

I am getting this error again and again, I have tried all the below commands but still getting this error

cmd use:

  1. npm install @prisma/client
  2. npm install prisma --save-dev
  3. npx prisma generate
  4. npm run dev

I have uploaded this code on my GitHub repo. What is my mistake?

GitHub link: https://github.com/TanmayChaurasia24/TTM/tree/main/backend

1 Answer 1

1

I see that you are trying to define Prisma outside the context, there is nothing wrong with your approach, although there are reason as to why you should not do that.

Defining Prisma outside the context in Hono (or any framework) is discouraged because it can lead to connection pooling issues, especially in serverless environments. If Prisma is defined globally, it may hold onto stale connections, leading to failed queries or exhausted connection pools. By defining it inside the request context, you ensure fresh connections for each request. In traditional long-running servers, defining Prisma globally can work, but it's not ideal for serverless setups.

Found this article that can help you understand more: Is better pass prisma object through context to resolvers or use it directly?

Also here is a snippet as to how you should use Prisma:

export const getAllUsers = async (c: Context) => {
  const prisma = new PrismaClient({
    datasourceUrl: c.env.DATABASE_URL,
  }).$extends(withAccelerate());

  try {
    const res = await prisma.user.findMany();
    return c.json({
      users: res.map((user) => ({
        id: user.id,
        username: user.username,
        email: user.email,
      })),
    });
  } catch (error) {
    return c.body(`Internal server error: ${error}`, 500);
  }
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.