dhairyashah
Portfolio

Jan 31st, 2024

Error: prisma client is unable to run in an edge runtime. | Error Fixed for Next.js v14

Author Picture

Dhairya Shah

Software Engineer

Important Note

This error has been fixed in the latest version of prisma. Try updating prisma to version 5.9.1 or greater.

npm i -D prisma@latest npm i @prisma/client@latest

To fix this error in Prisma and Next.js v14. We have two ways to solve the error:

  1. Integrate with Prisma Accelerate
  2. Downgrade to v5.8.1 (not recommended) (without Prisma Accelerate)

Integrate with Prisma Accelerate

Head over to Prisma Accelerate and log in. After successfully setting up your account, click on New Project to create a project.

Accelerate create new project

Click on Enable Accelerate to enable the Prisma Accelerate.

Value of Database connection string

In the Database connection string input field, get the DIRECT_URL value from the existing .env and paste it.

Note: Don’t include any annotations in the Database connection string.

Choose the closest region to your users.

Now click on Enable Accelerate.

After enabling Accelerate, click on Generate API Key which will generate a new DATABASE_URL for your database.

API Key Generate

Copy the snippet and replace with DATABASE_URL in your project’s environment variables i.e. (.env)

Now run the following command to update and install the required dependencies.

npm install @prisma/client@latest @prisma/extension-accelerate

Update your Prisma client instance file, with following code:

// lib/db.ts
import { PrismaClient } from '@prisma/client/edge'
import { withAccelerate } from '@prisma/extension-accelerate'

declare global{
    var prisma: PrismaClient | undefined;
}

export const db = globalThis.prisma || new PrismaClient().$extends(withAccelerate());

if(process.env.NODE_ENV !== "production") globalThis.prisma = db;

If your application is not running on edge network, then update the PrismaClient import to the following code:

import { PrismaClient } from '@prisma/client'

Remove the node_modules from the root directory.

rm -rf node_modules

After removing the node_modules, run the following command,

npm install
npx prisma generate --no-engine

Following all this steps properly will fix the error.

If you don’t want to use Prisma Accelerate, than check out the next method that will work without using the Prisma Accelerate.

To fix this error, first remove the node_modules folder from the /root directory of the project.

rm -rf node_modules

Inside your terminal run the following command:

npm i -D [email protected]
npm i @prisma/[email protected]

This will change the versions of prisma and @prisma/client to ^5.8.1 in package.json file.

"dependencies": {
    "@prisma/client": "^5.8.1",
  },
  "devDependencies": {
    "prisma": "^5.8.1",
  }

Note: If you have any other dependencies in your package.json, run the following command:

npm install

Finally, generate Prisma client locally in node_modules folder.

npx prisma generate

This will fix the Error: prisma client is unable to run in an edge runtime. error.

Conclusion

I hope this article was helpful to you. Thanks for reading. Have a great day!