Title: Sentry not working with ESModules while bundled · Issue #6147 · getsentry/sentry-javascript · GitHub
Open Graph Title: Sentry not working with ESModules while bundled · Issue #6147 · getsentry/sentry-javascript
X Title: Sentry not working with ESModules while bundled · Issue #6147 · getsentry/sentry-javascript
Description: Is there an existing issue for this? I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues I have reviewed the documentation https://docs.sentry.io/ I am using the latest SDK release https://github.com/...
Open Graph Description: Is there an existing issue for this? I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues I have reviewed the documentation https://docs.sentry.io/ I am using th...
X Description: Is there an existing issue for this? I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues I have reviewed the documentation https://docs.sentry.io/ I am using th...
Opengraph URL: https://github.com/getsentry/sentry-javascript/issues/6147
X: @github
Domain: github.com
IPv4 address: 140.82.112.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Sentry not working with ESModules while bundled","articleBody":"### Is there an existing issue for this?\n\n- [X] I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues\n- [X] I have reviewed the documentation https://docs.sentry.io/\n- [X] I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases\n\n### How do you use Sentry?\n\nSentry Saas (sentry.io)\n\n### Which package are you using?\n\n@sentry/node\n\n### SDK Version\n\n7.17.4\n\n### Framework Version\n\nTypescript 4.8.4\n\n### Link to Sentry event\n\n_No response_\n\n### Steps to Reproduce\n\nI'm using Typescript configured to output esmodule files. The files are built and bundled using Vercel's `ncc`.\r\n\r\nWhen 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.\r\n\r\nI'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.\r\n\r\nUnfortunately, 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. \r\n\r\nYet, this lets the build fail again, and no tinkering with files can solve this.\n\n### Expected Result\n\nThe 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. \n\n### Actual Result\n\nStacktrace while running without changes in `node_modules`:\r\n```\r\nReferenceError: require is not defined\r\n at call (***/node_modules/@sentry/node/cjs/module.js:22:1)\r\n at _4 (***/node_modules/@sentry/utils/cjs/buildPolyfills/_optionalChain.js:30:1)\r\n at _4 (***/node_modules/@sentry/utils/cjs/stacktrace.js:178:1)\r\n at _optionalChain (***/node_modules/@sentry/utils/cjs/buildPolyfills/_optionalChain.js:30:1)\r\n at line (***/node_modules/@sentry/utils/cjs/stacktrace.js:178:1)\r\n at stackParser (***/node_modules/@sentry/utils/cjs/stacktrace.js:28:1)\r\n at parseStackFrames (***/node_modules/@sentry/node/cjs/eventbuilder.js:10:1)\r\n at exceptionFromError (***/node_modules/@sentry/node/cjs/eventbuilder.js:22:1)\r\n at eventFromUnknownInput (***/node_modules/@sentry/node/cjs/eventbuilder.js:70:1)\r\n at eventFromException (***/node_modules/@sentry/node/cjs/client.js:122:1)\r\n```\r\n\r\n\r\nStacktrace while running the test after the changes in `node_modules`:\r\n```\r\n(node:9821) Warning: To load an ES module, set \"type\": \"module\" in the package.json or use the .mjs extension.\r\n(Use `node --trace-warnings ...` to show where the warning was created)\r\n\r\n***/node_modules/@sentry/node/esm/index.js:1\r\nimport { Integrations, getMainCarrier } from '@sentry/core';\r\n^^^^^^\r\n\r\nSyntaxError: Cannot use import statement outside a module\r\n at Object.compileFunction (node:vm:352:18)\r\n at wrapSafe (node:internal/modules/cjs/loader:1033:15)\r\n at Module._compile (node:internal/modules/cjs/loader:1069:27)\r\n at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)\r\n at Module.load (node:internal/modules/cjs/loader:981:32)\r\n at Function.Module._load (node:internal/modules/cjs/loader:822:12)\r\n at ModuleWrap.\u003canonymous> (node:internal/modules/esm/translators:170:29)\r\n at ModuleJob.run (node:internal/modules/esm/module_job:198:25)\r\n at async Promise.all (index 0)\r\n at async ESMLoader.import (node:internal/modules/esm/loader:385:24)\r\n at async importModuleDynamicallyWrapper (node:internal/vm/module:437:15)\r\n at async formattedImport (***/node_modules/mocha/lib/nodejs/esm-utils.js:7:14)\r\n at async Object.exports.requireOrImport (***/node_modules/mocha/lib/nodejs/esm-utils.js:38:28)\r\n at async Object.exports.loadFilesAsync (***/node_modules/mocha/lib/nodejs/esm-utils.js:91:20)\r\n at async singleRun (***/node_modules/mocha/lib/cli/run-helpers.js:125:3)\r\n at async Object.exports.handler (***/node_modules/mocha/lib/cli/run.js:370:5)\r\n```\r\n\r\n_*** represent the process filepath which has been obfuscated._","author":{"url":"https://github.com/nickmansrob","@type":"Person","name":"nickmansrob"},"datePublished":"2022-11-07T23:29:35Z","url":"https://github.com/getsentry/sentry-javascript/issues/6147"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:8b3bcb76-9c11-b5d4-f4f4-2ea1cdb9d8d8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8FF2:AD997:2F46AF9:3D10C93:6B0414A9 |
| html-safe-nonce | c9e308bc6762cbb1f92976cf45dd11ee514f04db8258f7c0256a67f291563e29 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RkYyOkFEOTk3OjJGNDZBRjk6M0QxMEM5Mzo2QjA0MTRBOSIsInZpc2l0b3JfaWQiOiI0ODk5MTY4NDUyNTY4NzMxMjkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f5a9b56ee09ad3e8cbbfb16298a7b618a3bd151377f8f57d3604fb337519b099 |
| hovercard-subject-tag | issue:1439156823 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/getsentry/sentry-javascript/6147/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7d74de1a647531e36b65c993193a064c70235ac4678e0e76986afc7883c345fb/getsentry/sentry-javascript/issues/6147 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7d74de1a647531e36b65c993193a064c70235ac4678e0e76986afc7883c345fb/getsentry/sentry-javascript/issues/6147 |
| og:image:alt | Is there an existing issue for this? I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues I have reviewed the documentation https://docs.sentry.io/ I am using th... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | nickmansrob |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/getsentry/sentry-javascript git https://github.com/getsentry/sentry-javascript.git |
| octolytics-dimension-user_id | 1396951 |
| octolytics-dimension-user_login | getsentry |
| octolytics-dimension-repository_id | 3328572 |
| octolytics-dimension-repository_nwo | getsentry/sentry-javascript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 3328572 |
| octolytics-dimension-repository_network_root_nwo | getsentry/sentry-javascript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width