Support importing from "@prisma/client/edge" in ESM
See original GitHub issueProblem
Right now if you try to do import { PrismaClient } from '@prisma/client/edge'
, you end up with:
node index.js
node:internal/process/esm_loader:94
internalBinding('errors').triggerUncaughtException(
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '$HOME/src/github.com/prisma/issue-edge/node_modules/@prisma/client/edge' imported from $HOME/src/github.com/prisma/issue-edge/index.js
Did you mean to import @prisma/client/edge.js?
at new NodeError (node:internal/errors:371:5)
at finalizeResolution (node:internal/modules/esm/resolve:418:11)
at moduleResolve (node:internal/modules/esm/resolve:983:10)
at defaultResolve (node:internal/modules/esm/resolve:1080:11)
at ESMLoader.resolve (node:internal/modules/esm/loader:530:30)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:251:18)
at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:79:40)
at link (node:internal/modules/esm/module_job:78:36) {
code: 'ERR_MODULE_NOT_FOUND'
}
I believe this is because the generated edge is a file, not a directory
node_modules/@prisma/client
├── LICENSE
├── README.md
├── edge.d.ts
├── edge.js
├── generator-build
│ └── index.js
├── index-browser.js
├── index.d.ts
├── index.js
├── package.json
Maybe it should be a directory? So edge/index.js
? Though I did think require’s resolution would try appending .js
, so I’m a bit confused about this still.
Suggested solution
It would be nice to support the shorter name. I’m guessing it’s documented like this already in certain places.
Alternatives
Leave it as is, we document and recommend the following instead:
import { PrismaClient } from "@prisma/client/edge.js"
console.log(PrismaClient)
Steps to Reproduce
prisma/schema.prisma
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
model User {
id Int @id
}
index.js
import { PrismaClient } from "@prisma/client/edge"
console.log(PrismaClient)
/bin/bash
node index.js
Versions
$ node -v
v16.14.2
$ npx prisma version
Environment variables loaded from prisma/.env
prisma : 3.15.1
@prisma/client : 3.15.1
Current platform : darwin
Query Engine (Node-API) : libquery-engine 461d6a05159055555eb7dfb337c9fb271cbd4d7e (at node_modules/@prisma/engines/libquery_engine-darwin.dylib.node)
Migration Engine : migration-engine-cli 461d6a05159055555eb7dfb337c9fb271cbd4d7e (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core 461d6a05159055555eb7dfb337c9fb271cbd4d7e (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary : prisma-fmt 461d6a05159055555eb7dfb337c9fb271cbd4d7e (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash : 461d6a05159055555eb7dfb337c9fb271cbd4d7e
Studio : 0.462.0
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Error importing PrismaClient in code compiled from typescript ...
The requested module '@prisma/client' is a CommonJS module, which may not support all module.exports as named exports.
Read more >Database access on the Edge with Next.js, Vercel and Prisma ...
Learn how you can query databases in Edge environments using Prisma ... Prisma Client's import from @prisma/client to @prisma/client/edge to ...
Read more >nexus-prisma - npm
ESM Support ; Refined DX ... @remarks Nexus Prisma imports Prisma client internally for two ... Nexus Prisma supports both ESM and CJS....
Read more >Prisma plugin for Pothos GraphQL
clientOutput : Where the generated code will import the PrismaClient from. ... a type only import will help avoid issues with undeclared //...
Read more >Modern application development with Prisma, GraphQL (or ...
Prisma Client : Auto-generated and type-safe database client ... UNIQUE_DB_SERVER_NAME --name prisma-azure-example --service-objective Basic.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Adding a folder with
package.json
won’t help:This happens because directory imports don’t exist in ESM, and
@prisma/client/edge
will not be considered a package even if you putpackage.json
there — thatpackage.json
will never even be read because it will be determined that the only relevant package is@prisma/client
— see step5.2
of thePACKAGE_RESOLVE
algorithm in the Node.js Resolver Algorithm Specification.The proper way to solve this specific problem would have been using subpath exports.
However, even after doint that, you would still not be able to import it like this:
You would have to do this instead:
This is not a problem specific to the edge client, the same happens (and has always happened) with the non-edge or non-DataProxy client too.
Everything becomes even more complicated once we also consider clients generated in a custom directory.
In other words, really the problem here is much more general and is not limited just to the to the edge client. The underlying issue is that we plainly don’t support ESM. Although Node.js does provide interoperability and allows to use Prisma from ES modules, it doesn’t work the same way as we document it for CJS.
I’m going to go ahead and change this to
kind/feature
. This will then need to be tackled as a part of a bigger project for ESM support.+1