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.

Running Sentry with Node.js 13+ ESM

See original GitHub issue

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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
tommedemacommented, Apr 25, 2020

Having issues with importing as es module too, using rollup:

rollup v2.7.2
bundles modules/background-app.js → dist/background-script.min.js...
[!] Error: 'import' and 'export' may only appear at the top level
node_modules/@sentry/utils/esm/misc.js (1:0)
1: import { isString } from './is';
   ^
2: import { snipLine } from './string';
3: /**
Error: 'import' and 'export' may only appear at the top level
    at error (/Users/tommedema/Projects/bubbles-chrome-extension/node_modules/rollup/dist/shared/rollup.js:161:30)
    at Module.error (/Users/tommedema/Projects/bubbles-chrome-extension/node_modules/rollup/dist/shared/rollup.js:14986:16)
    at tryParse (/Users/tommedema/Projects/bubbles-chrome-extension/node_modules/rollup/dist/shared/rollup.js:14876:23)
    at Module.setSource (/Users/tommedema/Projects/bubbles-chrome-extension/node_modules/rollup/dist/shared/rollup.js:15280:30)
    at /Users/tommedema/Projects/bubbles-chrome-extension/node_modules/rollup/dist/shared/rollup.js:17313:20
    at async Promise.all (index 5)
    at async Promise.all (index 3)
    at async Promise.all (index 9)
    at /Users/tommedema/Projects/bubbles-chrome-extension/node_modules/rollup/dist/shared/rollup.js:17162:28
    at async Promise.all (index 0)

@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

0reactions
kamilogorekcommented, Aug 27, 2021

@kamilogorek why did you close this ticket?

LRU has been fixed so there should be no more issues with it. If there is one, please open a new issue, thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.js - Sentry Documentation
On this page, we get you up and running with Sentry's SDK, so that it will automatically report errors and exceptions in your...
Read more >
How to use @sentry/node in razzle express server file
These deps seem to work now with universal js apps "@sentry/browser": "6.8.0", "@sentry/node": "6.8.0", "@sentry/tracing": "6.8.0".
Read more >
@sentry/node | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
How to Add Sentry to Your Node.js Project with TypeScript
Begin by navigating to Sentry and clicking "Get Started". You will be taken to the account creation screen: Sentry's account creation screen.
Read more >
Sentry for Ionic/Vue help
node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js in ... node scripts/install.js [17:38:26]: ▸ info sentry-cli Downloading ...
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