node-newrelic: ESM loader in v11 does not work with got package, after installing the latest import-in-the-middle
Description
Attempting to use v11 in an ESM project that includes got causes the node process to crash.
In Node v18 you will get a silent exit code 13. In Node v20, it will throw a SyntaxError before exit code 1.
app-1 | (node:8) ExperimentalWarning: `--experimental-loader` may be removed in the future; instead use `register()`:
app-1 | --import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; register("newrelic/esm-loader.mjs", pathToFileURL("./"));'
app-1 | (Use `node --trace-warnings ...` to show where the warning was created)
app-1 | file:///app/node_modules/@payzen/got-commons/node_modules/got/dist/source/index.js?iitm=true:39
app-1 | let $default = _.default
app-1 | ^
app-1 |
app-1 | SyntaxError: Identifier '$default' has already been declared
app-1 | at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:155:18)
app-1 | at callTranslator (node:internal/modules/esm/loader:285:14)
app-1 | at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:291:30)
app-1 |
app-1 | Node.js v20.10.0
app-1 exited with code 1
Expected Behavior
Running node --loader newrelic/esm-loader.mjs should work without crashing node.
NOTE: # ( Tell us what you expected to happen. )
Troubleshooting or NR Diag results
Steps to Reproduce
- run
npm init -y - run
npm i --save got newrelic - create the file index.mjs with the contents
import got from 'got'
- run
node --loader newrelic/esm-loader.mjs index.mjs - crash
Your Environment
- ex: Browser name and version: not relevant
- ex: Node version: 20.10.0
- ex: Operating System and version: Linux
Additional context
About this issue
- Original URL
- State: closed
- Created 6 months ago
- Comments: 24 (11 by maintainers)
I’ve tried your fix and the error is now different, it looks like the module loading is okay, but not all the exports are re-exported correctly, it looks like the exported object is got’s
Requestrather than the got object itself, I’m usinggot@14.0.0which is a native ESM module.Printing the default exports yields
Thank you @ljharb. It’s a problem with IITM. I wrote a test for core and it passes:
Core Test
You can’t export default multiple times, so it’s possible this is a node loader bug.