Skip to content

Sentry not working with ESModules while bundled #6147

Closed
@nickmansrob

Description

@nickmansrob

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which package are you using?

@sentry/node

SDK Version

7.17.4

Framework Version

Typescript 4.8.4

Link to Sentry event

No response

Steps to Reproduce

I'm using Typescript configured to output esmodule files. The files are built and bundled using Vercel's ncc.

When an error occurs, the console outputs a Sentry error: ReferenceError: require is not defined. Full stacktrace below. The filepaths in the trace indicate that, while all packages are built using ESM style, Sentry still uses it's CommonJS files, and because require doesn't exist in the ESM world, this logically throws this error.

I've solved this issue by tampering Sentry's files in node_modules. In each package's package.json, I've replaced the line "main": "cjs/index.js", to "main": "esm/index.js",. This keeps the server running and sends all errors correctly to the Sentry Dashboard.

Unfortunately, the changes in the node_module folder result in letting our local test runner fail with SyntaxError: Cannot use import statement outside a module. Full stacktrace below. I've investigated those errors and made some extra changes in 'node_modules'. Inserting "type": "module" to each package.json and appending /index.js to each import of @sentry/utils/esm/buildPolyfills makes the tests succeed.

Yet, this lets the build fail again, and no tinkering with files can solve this.

Expected Result

The Sentry packages should auto-select the module property in each package.json and following the ESM rules in its ESM files. This results in proper usage of ESM compliant files and thus works as it should.

Actual Result

Stacktrace while running without changes in node_modules:

ReferenceError: require is not defined
    at call (***/node_modules/@sentry/node/cjs/module.js:22:1)
    at _4 (***/node_modules/@sentry/utils/cjs/buildPolyfills/_optionalChain.js:30:1)
    at _4 (***/node_modules/@sentry/utils/cjs/stacktrace.js:178:1)
    at _optionalChain (***/node_modules/@sentry/utils/cjs/buildPolyfills/_optionalChain.js:30:1)
    at line (***/node_modules/@sentry/utils/cjs/stacktrace.js:178:1)
    at stackParser (***/node_modules/@sentry/utils/cjs/stacktrace.js:28:1)
    at parseStackFrames (***/node_modules/@sentry/node/cjs/eventbuilder.js:10:1)
    at exceptionFromError (***/node_modules/@sentry/node/cjs/eventbuilder.js:22:1)
    at eventFromUnknownInput (***/node_modules/@sentry/node/cjs/eventbuilder.js:70:1)
    at eventFromException (***/node_modules/@sentry/node/cjs/client.js:122:1)

Stacktrace while running the test after the changes in node_modules:

(node:9821) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)

***/node_modules/@sentry/node/esm/index.js:1
import { Integrations, getMainCarrier } from '@sentry/core';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1033:15)
    at Module._compile (node:internal/modules/cjs/loader:1069:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:170:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:437:15)
    at async formattedImport (***/node_modules/mocha/lib/nodejs/esm-utils.js:7:14)
    at async Object.exports.requireOrImport (***/node_modules/mocha/lib/nodejs/esm-utils.js:38:28)
    at async Object.exports.loadFilesAsync (***/node_modules/mocha/lib/nodejs/esm-utils.js:91:20)
    at async singleRun (***/node_modules/mocha/lib/cli/run-helpers.js:125:3)
    at async Object.exports.handler (***/node_modules/mocha/lib/cli/run.js:370:5)

*** represent the process filepath which has been obfuscated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Package: nodeIssues related to the Sentry Node SDK

    Type

    No type

    Projects

    Status

    No status

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions