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 importing from "@prisma/client/edge" in ESM

See original GitHub issue

Problem

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:open
  • Created a year ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
aqrlncommented, Aug 29, 2022

Adding a folder with package.json won’t help:

Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/Users/aqrln/prisma/tmp/node_modules/@prisma/client/edge' is not supported resolving ES modules imported from /Users/aqrln/prisma/tmp/index.js
Did you mean to import /Users/aqrln/prisma/prisma/packages/client/edge/index.js?
    at new NodeError (node:internal/errors:387:5)
    at finalizeResolution (node:internal/modules/esm/resolve:400:17)
    at moduleResolve (node:internal/modules/esm/resolve:965:10)
    at defaultResolve (node:internal/modules/esm/resolve:1173:11)
    at nextResolve (node:internal/modules/esm/loader:173:28)
    at ESMLoader.resolve (node:internal/modules/esm/loader:852:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:439:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)
    at link (node:internal/modules/esm/module_job:75:36) {
  code: 'ERR_UNSUPPORTED_DIR_IMPORT',
  url: 'file:///Users/aqrln/prisma/tmp/node_modules/@prisma/client/edge'
}

This happens because directory imports don’t exist in ESM, and @prisma/client/edge will not be considered a package even if you put package.json there — that package.json will never even be read because it will be determined that the only relevant package is @prisma/client — see step 5.2 of the PACKAGE_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:

import { PrismaClient } from "@prisma/client/edge"

You would have to do this instead:

import PrismaEdge from "@prisma/client/edge"
const { PrismaClient } = PrismaEdge

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.

0reactions
insomnia-creatorcommented, Nov 24, 2022

+1

Read more comments on GitHub >

github_iconTop 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 >

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