Running Sentry with Node.js 13+ ESM
See original GitHub issue- Review the documentation: https://docs.sentry.io/
- Search for existing issues: https://github.com/getsentry/sentry-javascript/issues
- Use the latest release: https://github.com/getsentry/sentry-javascript/releases
- ~[ ] Provide a link to the affected event from your Sentry account~ I am setting it up, before getting to my user account.
Package + Version
-
@sentry/browser
-
@sentry/node
-
raven-js
-
raven-node
(raven for node) - other:
Version:
5.10.2
Description
This is my trip to get Sentry to work with Rollup and Node.js 13 (ESM).
LRUMap Bug (fixed)
The first error, bundling Sentry/Node with Rollup is:
[!] Error: 'LRUMap' is not exported by node_modules/lru_map/lru.js
https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module
node_modules/@sentry/node/esm/parsers.js (3:9)
1: import { basename, dirname, snipLine, SyncPromise } from '@sentry/utils';
2: import { readFile } from 'fs';
3: import { LRUMap } from 'lru_map';
^
4: import * as stacktrace from './stacktrace';
5: // tslint:disable-next-line:no-unsafe-any
Error: 'LRUMap' is not exported by node_modules/lru_map/lru.js
at error (<PROJECT>/node_modules/rollup/dist/rollup.js:5365:30)
at Module.error (<PROJECT>/node_modules/rollup/dist/rollup.js:9708:9)
at handleMissingExport (<PROJECT>/node_modules/rollup/dist/rollup.js:9625:21)
at Module.traceVariable (<PROJECT>/node_modules/rollup/dist/rollup.js:10018:17)
at ModuleScope.findVariable (<PROJECT>/node_modules/rollup/dist/rollup.js:8685:39)
at Identifier$1.bind (<PROJECT>/node_modules/rollup/dist/rollup.js:4394:40)
at NewExpression.bind (<PROJECT>/node_modules/rollup/dist/rollup.js:3143:23)
at NewExpression.bind (<PROJECT>/node_modules/rollup/dist/rollup.js:7948:15)
at VariableDeclarator.bind (<PROJECT>/node_modules/rollup/dist/rollup.js:3143:23)
at VariableDeclaration.bind (<PROJECT>/node_modules/rollup/dist/rollup.js:3139:31)
After some debugging, I found the correct setup to make it work, so I thought I’d share it here. So in rollup.config.js
, you have to setup the commonjs
plugin (rollup-plugin-commonjs
) to use a Named Export:
import commonjs from "rollup-plugin-commonjs";
// I already had these two:
import nodeResolve from "rollup-plugin-node-resolve";
import json from "rollup-plugin-json";
export default {
input: "index.js",
output: {
file: "index.min.js",
format: "esm"
},
plugins: [
nodeResolve({ preferBuiltins: true }),
commonjs({
namedExports: {
lru_map: ["LRUMap"]
}
}),
json()
]
};
Import Sentry from ‘@sentry/node’ (fixed)
Then I also got a Error: 'Sentry' is not exported by node_modules/@sentry/node/esm/index.js
error. This is a lot easier, you have to import all as Sentry:
// index.js
import * as Sentry from '@sentry/node';
// NOT import Sentry from '@sentry/node';
// NOT import { Sentry } from '@sentry/node';
Require is not defined (NOT fixed)
Now that the bundle is working, let’s try to launch the server. But upon npm start
, I’m getting this error:
(node:14916) ExperimentalWarning: The ESM module loader is experimental.
file:///<PROJECT>/index.min.js:20544
var mainModule = ((require.main && require.main.filename && dirname(require.main.filename)) ||
^
ReferenceError: require is not defined
at file:///<PROJECT>/index.min.js:20544:18
at ModuleJob.run (internal/modules/esm/module_job.js:109:37)
at async Loader.import (internal/modules/esm/loader.js:133:24)
This error comes from line 20544
, which looks like this:
var mainModule = ((require.main && require.main.filename && dirname(require.main.filename)) ||
global.process.cwd()) + "/";
This line seems to come from raven-node
:
https://github.com/getsentry/sentry-javascript/blob/master/packages/raven-node/lib/utils.js#L252
Note that running node sentry.js
straight away works, but the bundled version does not work.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:7 (2 by maintainers)
Top GitHub Comments
Having issues with importing as es module too, using rollup:
@HazAT what you should do is make a browser package like sentry compatible with latest standards, in this case es module imports and exports. If you use webpack, you can configure it to export es modules
LRU has been fixed so there should be no more issues with it. If there is one, please open a new issue, thanks