Rethink dependency auto-install during Prisma Client's generation
See original GitHub issueI’m specifically talking about this part of the Prisma Client spec: https://github.com/prisma/specs/tree/master/cli#install-prismaclient
I’m told that the reasoning behind introducing it was to smoothen the first user experience, but I urge you to rethink our solution.
Here are the problems I see currently:
-
This smells like an anti-pattern.
prisma2 generateis touching and modifying files outside its own “scope”. This feels like an abuse of the lack of a permissions system in Node. It is notprisma2’s responsibility to manage dependencies. In less technical terms, it is too “magical”. -
We’re making assumptions about the end user’s setup. They might be working with yarn/npm workspaces, might have other symlinks in their
node_modulesthat will be disturbed by ayarn installcommand, or they might not even be usingnpmoryarnto manage dependencies at all (thinkpnpm,lernaoryarn2) The point is, we don’t have enough information to figure all this out, and we shouldn’t need to. As an example: In Studio’s tests, I use the SDK to generate the Client (in other words, the programmatic API of Prisma Client). -
This is not CI / test friendly. If I run
prisma2 generateduring a test, it will have a side-effect of modifying mypackage.jsonafter it has run. This might not be as significant, but it highlights the fact that our current solution does not take a lot of use cases into account.
Here are some solutions I propose. Please correct me if I’m missing some context:
- When I run
prisma2 generatein a project where we’re able to find aschema.prismafile but the generator cannot be resolved, error out. If the error clearly tells me that I need to install a separate package to use thegeneratorI have defined in myprisma.schema, I do not see it as a friction point in our “Getting Started” experience.
(Moving 2. to prisma/specs#452 because I don’t think it belongs here on second thought)
2. Instead of going back to the old way of running prisma2 generate in postinstall hooks, we could show a warning in development mode about Prisma Client being “out of sync” when it detects a schema.prisma change. This could happen during the Client’s first connect() call, which happens automatically.
(AFAICT, a nasty side-effect of a failed postinstall script in a package is that the user cannot even npm i without getting rid of the faulty package, which is why I’m against a postinstall script in the first place.)
We might also be able to look at tools like Prettier and run prisma2 generate whenever schema.prisma changes as part of our VS Code plugin. This obviously won’t work for people on Sublime etc., though.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:9
- Comments:8 (6 by maintainers)

Top Related StackOverflow Question
This appears to have been re-introduced: https://github.com/prisma/prisma/blob/b337d3c2bf3c4278da45912b8ca5cdc9a64ab894/src/packages/sdk/src/predefinedGeneratorResolvers.ts#L78
While it certainly is helpful in simple setups, I still think the points made by @madebysid are valid, specifically point 2
I’ve have two problems with this behavior so far:
@prisma/cliin my root workspace and only have@prisma/clientin my service workspaces, this kept putting@prisma/cliinto mypackage.jsons. This was slightly annoying since I was trying to only keep runtime dependencies in the last stage of my docker build.@prisma/clientwrapper package.Related https://github.com/prisma/prisma/issues/1439#issuecomment-698398513
Just to try and add some additional context we have unfortunately found prisma within a monorepo context somewhat untenable. Slightly exagerating the scope of the original issue here but it goes to highlight how decisions made for ease of first usage don’t scale well. Using
Nxto manage tasks within a basicnpm workspacesrepo, we have found the following pain points:@prisma/clientis empty and throws an exception when something tries to resolve it. luckily we just change our imports, but…__dirnameis used excessively and suddenly we have to copy schema files, binaries and runtimes all over the place to keep a bundle portable…prisma generatewhich confused many of us (and is actually how I ended up on this issue in the first place trying to figure out what was happening)To throw a few other pains in which really become noticeable in a monorepo setting:
(a as Prisma.Decimal) instanceof (b as Decimal)client/index.jsgrows linearly and without any use ofes modules(or at least#__purecomments) reduces the ability to effectively optimise bundles (particularly a serverless function setting)This article about creating an nx generator for prisma really shows the length needed to go to in order to get prisma clients functioning predictably in a monorepo - and the result of “there is one magic package that provides a gateway into other individual schemas” defeats the purpose of being able to make a granular monorepo with small schemas/clients.
Suggestions:
@prisma/client- let the developer generate where they want and import however they want - it’s fundamentally broken (I never say this lightly, I’ve struggled with it for months to finally understand what the issue is) to have an installable package that is then expected to be mutated by the consumer - a package forces a to-one dependency whereas the reality is we always want to-many dependency on N clientsSorry this turned into a bit of a dump but hopefully it helps somebody on the prisma team understand the problems in a more fundamental way. I’d be very happy to hop into slack and discuss further - even contribute if there’s a clear path!