0

Bug description Hello, I am new to Prisma. I have been trying to access Prisma database with nextjs. And every time I try to use PrismaClient from @prisma/client in a nextjs page, I have been seeing this error.

./node_modules/@prisma/client/runtime/index.js:26236:39 Module not found: Can't resolve 'async_hooks' null

How to reproduce Steps to reproduce the behavior:

My Schema file

datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}

generator client {
provider = "prisma-client-js"
}

model User {
id Int @default(autoincrement()) @id
email String @unique
name String
password String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}

I created a migration and ran with the following commands

npx prisma migrate save --experimental npx prisma migrate up --experimental

Then I also run npx prisma generate

This is my register.tsx file where I am accessing the prisma client

import { useState } from "react";
import { useRouter } from "next/router";
import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

interface RegisterProps {}

const Register: React.FC = ({}) => {
const router = useRouter();
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [name, setName] = useState("");

const submitHandler = async (e) => {
e.preventDefault();
try {
const user = await prisma.user.create({
data: {
email,
password,
name,
},
});
console.log(user);
router.push("/");
} catch (e) {
console.log(e);
}
};

return (
// my template goes here
);
};

export default Register;

After running this, I am getting this following error

/node_modules/@prisma/client/runtime/index.js Module not found: Can't resolve 'async_hooks' null

Expected behavior I expect this code to build my page first. But it fails to do so and shows me the error in the title.

Prisma information All of these information was included in the steps to reproduce part

Environment & setup OS: Ubuntu Database: MySQL Node.js version: 14.05 Prisma version: 2.11.0

1 Answer 1

2

It's not possible to use PrismaClient directly on the frontend. You can only call it in the server-side methods that NextJS supports like this.

Sign up to request clarification or add additional context in comments.

4 Comments

So how do I create data in the database with prisma? Because I need the state values to login and register without using the api
I don't think it's possible without the API.
This is for sure? we cant use prisma client on FE this tutorial shows like it can be daily.dev/blog/nextjs-with-prisma
@MarkoBlazekovic Next.js is server-rendered, so Prisma can be used only parts that are run on the server, i.e. getServerSideProps or getStaticProps.

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.