Henrik VT
Henrik VT

Reputation: 533

Using Prisma with Yarn v3

I am building an app using Next.js, Next-Auth, and Prisma. I am using API Routes for backend code. Recently, I upgraded Yarn on my pc to v3.1.1. I then created a .yarnrc.yml file and set the nodeLinker option to "node-modules" to avoid converting this project to PnP just yet.
When I run the dev server to work on my application, I run into this error as it tries to compile the next-auth API Route. Weirder yet, this issue does not occur when deployed via Vercel; the function logs are just fine and it works as expected.

Error: ENOENT: no such file or directory, open 'C:\Users\htech\Desktop\code\consulting\.yarn\unplugged\@prisma-client-virtual-d7adb716b6\node_modules\.prisma\client\schema.prisma'

When I search for similar issues, the usual solution that arises is setting a custom output path in schema.prisma. When I add a custom output path and update the imports accordingly, it just spits out some esoteric internal dependency error.

error - ./prisma/generated/runtime/index.js:27652:17
Module not found: Can't resolve '_http_common'
  27650 |   "../../node_modules/.pnpm/[email protected]/node_modules/undici/lib/node/http-parser.js"(exports2, module2) {
  27651 |     "use strict";
> 27652 |     var common = require("_http_common");

Environment


This seems to be a simple issue with where it's generating to. However, I have no idea how to go about solving this issue, short of rolling back my systemwide version of yarn. Have I ran into a bug? Does anyone have a solution?

Upvotes: 1

Views: 4604

Answers (2)

Henrik VT
Henrik VT

Reputation: 533

Update for June 2024

See original solution below.
There are two options today that would work without disabling Yarn Plug'n'Play.

  1. Custom Prisma Output Directory
    Prisma allows configuration of where it places the files of the generated client. To do so, set the output property in the generator block. It is best to also set the same path in your .gitignore.
generator client {
  provider = "prisma-client-js"
  output   = "../src/generated/client"
}

Full Docs: https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/generating-prisma-client#using-a-custom-output-path

  1. Yarn PnPify
    This acts as a middleman to redirect Prisma's output. See the comment from Moses Gamelli below for instructions.

After having tested a number of different things, I believe I have found a solution.

The Problem

The problem lies in how Yarn is resolving imports. Prisma CLI generates the client (by default) to node_modules/.prisma. Even when nodeLinker is set to node-modules, Yarn tries to resolve this import to .yarn/unplugged/<folder-name>, where the generated client does not exist.

The Solution

In order to fix this resolution problem, we have to create a custom resolution for the .prisma folder. This assumes that you do not have a custom output path.

# .yarnrc.yml

nodeLinker: node-modules

packageExtensions:
  "@prisma/client@*":
    dependencies:
      ".prisma": 'link:See "resolution" field of package.json'
# package.json

{
  ...
  "resolutions": {
     ".prisma": "link:node-modules/.prisma"
  }
}

Credit to this user on Github

Upvotes: 5

Moses Gameli
Moses Gameli

Reputation: 61

If you're using yarn with pnp enabled:

  1. Install @yarnpkg/pnpify to dev dependencies
  2. Prepend yarn pnpify with any prisma command e.g yarn pnpify prisma generate
  3. Consider using a custom output path in your prisma schema

Upvotes: 2

Related Questions