How to use newrelic with esm "import" syntax?
See original GitHub issuehttps://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:
- Created 3 years ago
- Reactions:13
- Comments:14 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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'
toconst DEFAULT_FILENAME = 'newrelic.cjs'
And then in the root of my project, renamed
newrelic.js
tonewrelic.cjs
Then imported newrelic atimport * as newrelic from 'newrelic'
in root index.jsI know this is not a recommended way, but it was the only was I could make it work at the moment.
@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.