Skip to content

Add esm support to @prisma/instrumentation #23410

Closed

Description

@AbhiPrasad

Problem

@prisma/instrumentation is not ESM compatible.

https://github.com/prisma/prisma/blob/caeb9ba88cba8bafca8654e2c903160aa16a3fd5/packages/instrumentation/src/constants.ts#L1

The require call above is one part of it, but the other is that the package needs to be emitted with type: module or *.mjs.

Suggested solution

Make the package ESM compatible.

Activity

  1. added
    kind/feature
    tech/typescript
    on Mar 13, 2024
  2. SevInf commented on Mar 14, 2024

    @SevInf
    Contributor

    Considering prisma client itself is not yet ESM-compatible, what benefits would converting just @prisma/instrumentation bring?

  3. timfish commented on Mar 14, 2024

    @timfish

    Outside of any potential ESM issues, the require('../package.json') code makes @prisma/instrumentation incompatible with many bundlers out of the box.

    The next version of @sentry/node will hopefully have @prisma/instrumentation as a dependency so that instrumentation works without any special configuration but currently this will stop consumers of @sentry/node from bundling their code unless their bundler supports json. Even if their bundler does support json, the entire package.json will get included in the bundle.

  4. patjakubik commented on Apr 1, 2024

    @patjakubik

    @SevInf I'm not an expert by any means, but I'm currently moving my server build to ESM with esbuild and the only package that esbuild has issues with is @prisma/instrumentation. Prisma Client seems to be doing just fine.

  5. timfish commented on Apr 2, 2024

    @timfish

    There are two options that I can see:

    • Use readFileSync and JSON.parse instead of require so biundlers don't pick it up as a dependency. It'll need a try/catch and a fallback version as it will likely fail to find the file if the code gets bundled
    • Have a version string placeholder in the code which gets overwritten before publish
  6. AbhiPrasad commented on Apr 15, 2024

    @AbhiPrasad
    Author

    @SevInf would you accept PRs for this change? Or could we get it prioritized in another way? We'd love to have Prisma instrumentation out-of-the-box for Sentry, this would help a lot!

  7. timfish commented on Apr 24, 2024

    @timfish

    Just checked the latest release and the bundling of @prisma/instrumentation means there's no longer a require('../package.json') in the distributed code any more.

    Thanks!

  8. ericallam commented on Jul 15, 2024

    @ericallam

    Providing an ESM build (alongside the CJS build) would be helpful for codebases that use ESM and import @prisma/instrumentation. Currently when importing @prisma/instrumentation in a fresh Node.js/ESM project, importing does not work with named exports:

    import { PrismaInstrumentation } from '@prisma/instrumentation'

    Results in the following error:

    import { PrismaInstrumentation } from "@prisma/instrumentation";
             ^^^^^^^^^^^^^^^^^^^^^
    SyntaxError: Named export 'PrismaInstrumentation' not found. The requested module '@prisma/instrumentation' is a CommonJS module, which may not support all module.exports as named exports.
    CommonJS modules can always be imported via the default export, for example using:
    
    import pkg from '@prisma/instrumentation';
    const { PrismaInstrumentation } = pkg;
    
        at ModuleJob._instantiate (node:internal/modules/esm/module_job:134:21)
        at async ModuleJob.run (node:internal/modules/esm/module_job:217:5)
        at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
        at async loadESM (node:internal/process/esm_loader:28:7)
        at async handleMainPromise (node:internal/modules/run_main:113:12)
    
    Node.js v20.12.2
    
  9. champikasam commented on Sep 19, 2024

    @champikasam

    I am seeing the same error as @ericallam . I have imported the full module as suggested there. Would be good to have a named export working.

  10. kentcdodds commented on Feb 4, 2025

    @kentcdodds

    Just tossing my experience in the ring as well. I had a failed build because of this, and here's what I had to do: epicweb-dev/epic-stack@b647d19

  11. added this to the 6.7.0 milestone on Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/featuretech/typescripttopic: esmtopic: tracing

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions