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.

How to use newrelic with esm "import" syntax?

See original GitHub issue

https://nodejs.org/docs/latest-v13.x/api/esm.html#esm_enabling

When enabling esm mode, either import is recognized or require is recognized. Both are not recognized together, so is there a way to the import syntax instead of require? (I’m unwilling to convert my entire app back to the require format just for this)

I’d like to use this:

import 'newrelic.js';

I tried this:

import 'newrelic.cjs';

(explicitly making it cjs = use common module for one file)

Which currently gives an error:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:\Projects\backend\src\bin\www.js
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1163:13)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:13
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
HishamMubarakcommented, Apr 24, 2021

Ran into this same issue after adding "type": "module" in my package.json file.

The only solution I found was to change code inside newrelic package using patch-package

Changed content inside /node_modules/newrelic/lib/config/index.js at line 35: const DEFAULT_FILENAME = 'newrelic.js' to const DEFAULT_FILENAME = 'newrelic.cjs'

And then in the root of my project, renamed newrelic.js to newrelic.cjs Then imported newrelic at import * as newrelic from 'newrelic' in root index.js

I know this is not a recommended way, but it was the only was I could make it work at the moment.

1reaction
carlo-808commented, Mar 31, 2021

@drmrbrewer If we get to supporting ESM in the agent, we will ensure that the config will load via ESM import. That being said, we have several roadmap items ahead of that work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using the Node.js agent in an ES module application
How to install and configure your Node.js APM agent for an application that uses ES modules.
Read more >
How can import the 'got' module in a script compatible with the ...
SyntaxError : Cannot use import statement outside a module. So, google and stack overflow tell me I need to make my script into...
Read more >
Import Node.js modules - New Relic Documentation
Extend your synthetic script environment by importing popular Node.js modules. ... You can use any node module in a containerized private minion.
Read more >
Node.js agent v9.1.0 - New Relic Documentation
Enabled re-throwing ESM import errors of newrelic.js so that the user is informed to rename it to newrelic.cjs.
Read more >
Requiring 'newrelic' a second time doesn't return an instance ...
To use the API, even when using node -r , you'll need to import newrelic ... To import, your statement will likely need...
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