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.

`prisma generate` fails in yarn2 / yarn3 workspaces

See original GitHub issue

Bug description

We are using prisma in a yarn2 monorepo consisting of multiple workspaces. We were on version 2.23.0 before and everything was working fine. After upgrading to 2.29.0, prisma tries to run npm to install @prisma/client, even though it’s already installed in the current folder. This fails, as npm is unable to recognize the imported workspace.

yarn prisma generate
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
npm ERR! code EUNSUPPORTEDPROTOCOL
npm ERR! Unsupported URL Type "workspace:": workspace:core-api-plugins/article-import-edeka

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/valentinmorlock/.npm/_logs/2021-08-16T10_13_58_979Z-debug.log
Error: Command failed with exit code 1: npm install -D prisma@2.29.1

How to reproduce

clone this repo: https://github.com/v-morlock/prisma-yarn2-repro

yarn
cd workspace 
yarn prisma generate

Expected behavior

Prisma should generate its files

Prisma information

Environment & setup

  • OS: macOS
  • Database: mySQL
  • Node.js version: 16.1.0

Prisma Version

2.29.1, stopped working on 2.26.0

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:14
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

18reactions
lensbartcommented, Apr 2, 2022

For what it’s worth, the following changes were sufficient in my case. I’m using Prisma with Yarn 3 (zero-installs / plug ‘n’ play) in a monorepo.

I started from @nishils’s suggestion (which worked) and simplified it as much as I could while still keeping everything functional:

  1. Just install prisma and @prisma/client as per the docs
yarn add -D prisma
yarn add @prisma/client
  1. Change the output directory to something outside of node_modules:
generator client {
  output = "../src/__generated__/prisma"
}
  1. Use pnpify to be able to run the command-line script
yarn add @yarnpkg/pnpify
yarn pnpify prisma generate
5reactions
jpuleccommented, Nov 24, 2021

If anyone is interested here’s the patch we use to get prisma to work fully with PnP. It just swaps prisma’s bundled resolve for an actual require statement, and disables a sanity check that assumes the location of the prisma client, which doesn’t make sense in a PnP world. To get this to work, you’ll also need to add a packageExtension to prisma to get it to install resolve as a dependency. With all this, we have prisma working fully with PnP in a workspace set up. This patch is for prisma 2.30.3, but I’m guessing that you’d only need to perform similar patching in a newer version.

diff --git a/build/index.js b/build/index.js
index a34d93d52218ef223a8f6510b86778291fe5a9fa..0dfe9cf98f02033241f97b0710adbde371f1f1c1 100755
--- a/build/index.js
+++ b/build/index.js
@@ -34191,7 +34191,7 @@ var require_resolve2 = __commonJS((exports2) => {
     resolve: () => resolve,
     resolvePkg: () => resolvePkg2
   });
-  var import_resolve = __toModule3(require_resolve());
+  var import_resolve = __toModule3(require("resolve"));
   var import_path10 = __toModule3(require("path"));
   async function resolve(id, options) {
     const _options = {preserveSymlinks: false, ...options};
@@ -34259,9 +34259,6 @@ var require_predefinedGeneratorResolvers = __commonJS((exports2) => {
       return resolvedClientDir;
     if (clientDir === void 0)
       return resolvedClientDir;
-    const relDir = import_path10.default.relative(CLIDir, clientDir).split(import_path10.default.sep);
-    if (relDir[0] !== ".." || relDir[1] === "..")
-      return void 0;
     return resolvedClientDir;
   }
   var predefinedGeneratorResolvers = {
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Prisma with Yarn v3 - Stack Overflow
The problem lies in how Yarn is resolving imports. Prisma CLI generates the client (by default) to node_modules/.prisma . Even when nodeLinker ...
Read more >
Generating the client (Concepts) - Prisma
This page explains how to generate Prisma Client. It also provides additional context on the generated client, typical workflows and Node.js configuration.
Read more >
Building a Monorepo with Yarn 2 - Heroku Blog
Yarn 2 is officially supported by Heroku. Here's a popular use case for Yarn enhanced by Yarn 2: using workspaces to manage dependencies...
Read more >
Building a sane Docker image for Typescript, Yarn ... - Medium
Building a sane Docker image for Typescript, Yarn Workspaces and Prisma 2 · First, you can see that it's a multistage build. ·...
Read more >
JavaScript package managers compared: npm, Yarn, or pnpm?
Yarn Berry goes even further by ditching node_modules completely with its Plug'n'Play (PnP) mode. In this article, we'll cover the following ...
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