question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for Yarn 2+ with PnP

See original GitHub issue

When trying to run prisma2 init via yarn dlx it fails in the following way:

image

Getting the same behavior when having installed prisma2 as a local dev dependency:

image

I’m using yarn 2.0.0-rc.27

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:48
  • Comments:48 (11 by maintainers)

github_iconTop GitHub Comments

39reactions
dgatteycommented, Jan 28, 2022

Thank you @Globolobo + @darioblanco for putting me on the right path! I compiled this year-of-2022 note since no one suggestion above worked perfectly for me with Next + Vercel + Yarn 3 + Yarn PnP + Yarn Zero Install. Here’s my setup.


In package.json I use pnpify with a script to wrap generation. I also run generation as part of my yarn build. Nothing fancy with module resolution needed.

"scripts": {
    "build": "yarn db:generate && next build",
    "db:generate": "yarn pnpify prisma generate",
},
"dependencies": {
    "@prisma/client": "3.8.1",
},
"devDependencies": {
    "@yarnpkg/pnpify": "^3.1.1-rc.12",
    "prisma": "3.8.1",
  },

In my prisma/schema.prisma, I set a relative output path (this can be anything, I’m just putting it into my server api folder). It just needs to be within src.

generator client {
  provider = "prisma-client-js"
  output   = "../src/api/server/generated"
}

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

I make sure to ignore that path for version control (and prettier/eslint/anything else) in .gitignore/etc

# Generated files from Prisma
src/api/server/generated

And finally, in my .ts files, I just import from my custom path instead of from the main package. Works great!

import { PrismaClient } from 'api/server/generated/';
const dbClient = new PrismaClient();

or

import type { Prisma } from 'api/server/generated/'';

Notes

  1. If your package resolution requires a relative path like ../../api/... or a prefix like @app/api/..., that should work fine instead, I just use api/... directly since I set that up as a top level path.

  2. You could avoid putting the generate command in your build command if you check the generated folder into version control, but I didn’t think that was worth it. If you don’t check it in, then Vercel/whatever will fail the build since it’ll be missing until you run generation again. As my DB should always be up and accessible, I see no problem with generating from Vercel before I build. Catches breaking changes anyway.

14reactions
matthewmuellercommented, Aug 11, 2020

Quick update

We’d like to add support for Yarn 2, but not right now. Yarn 2 is a significant departure from other Node package managers (no node_modules!). By default, Prisma Client relies on the presence of node_modules, so we need to figure out how to first detect Yarn 2 and then apply a Yarn 2-specific solution.

This is all totally doable, but it will just take time. Right now we’re focused on adding more powerful query features to the Prisma Client.

PRs to fix this are very welcome of course!

Workaround

In the meantime, you can workaround this issue by providing a custom output path.

prisma/schema.prisma

generator client {
  provider = "prisma-client-js"
  // Output to a custom location
  output   = "../generated/client"
}

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

model User {
  id      Int      @default(autoincrement()) @id
  email   String   @unique
  name    String?
  posts   Post[]
  profile Profile?
}

main.ts

// Include the client from a custom location
import { PrismaClient } from './generated/client'

async function main() {
  const client = new PrismaClient()
  const data = await client.user.findMany()
  console.log(data)
  client.disconnect()
}

main().catch(console.error)

You can read more about this feature here: https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/generating-prisma-client#specifying-a-custom-output-path

Read more comments on GitHub >

github_iconTop Results From Across the Web

Plug'n'Play | Yarn - Package Manager
Unveiled in September 2018, Plug'n'Play is an innovative installation strategy for Node. Based on prior work in other languages (for example autoload for...
Read more >
Support Yarn 2 PnP · Discussion #4223 · vercel ... - GitHub
I think Vercel should make it clear that it works with Yarn 2+, as long as you don't use PnP. And Vercel should...
Read more >
Upgrade to Yarn >2 with (or without) Plug'n'Play | by Julien ...
A yarn install is even faster with PnP, because there is no node_modules directory to copy the zipped files into. Instead, with PnP...
Read more >
Yarn v2 PnP is simply a lifesaver if you have a medium+ sized ...
That said, I think yarn 2's PnP + zero installs ... relates to the fact that TS does not have native support for...
Read more >
Yarn 2, Yarn 3, PNP, and our migration journey - Jakub Zitny
At our second Productboard Frontend Meetup, the Software Engineer from Deepnote - Jakub Zitny, talked about Yarn 2, Yarn 3, PNP, and their ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found