elastic / elastic/apm-agent-nodejs
elastic-apm-node/start cannot load config with type 'module'
- Dominant language
- JavaScript
- Stars
- 594
- Forks
- 244
- Avg merge
- 1d 8h
- Merged PRs (30d)
- 16
Description
**Describe the bug**
When using `--experimental-modules` with the corresponding `"type": "module",` inside the package.json, the `elastic-apm-node/start` module is unable to load the config file:
```
Elastic APM initialization error: Can't read config file /home/luca/routeplanner-service/elastic-apm-node.js
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/luca/routeplanner-service/elastic-apm-node.js
require() of ES modules is not supported.
require() of /home/luca/routeplanner-service/elastic-apm-node.js from /home/luca/routeplanner-service/node_modules/elastic-apm-node/lib/config.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename elastic-apm-node.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/luca/routeplanner-service/package.json.
```
**To Reproduce**
Steps to reproduce the behavior:
1) Create a project that imports `elastic-apm-node/start`
2) Create the config file `elastic-apm-node.js`
2) Set the `type: "module"` setting in package.json
3) Launch project using `--experimental-modules`
**Expected behavior**
The `elastic-apm-node/start` is expected to load the configuration correctly using either the .cjs extension or making it available as an imported module itself.
**Environment (please complete the following information)**
- OS: WSL Ubuntu 18.04
- Node.js version: v13.7.0
- APM Server version: ?
- Agent version: 3.3.0
**How are you starting the agent? (please tick one of the boxes)**
- [ ] Calling `agent.start()` directly (e.g. `require('elastic-apm-node').start(...)`)
- [x] Requiring `elastic-apm-node/start` from within the source code
- [ ] Starting node with `-r elastic-apm-node/start`
**Additional context**
- Agent config options
Click to expand
```
{
// Override service name from package.json
// Allowed characters: a-z, A-Z, 0-9, -, _, and space
serviceName: 'routeplanner-service',
// Use if APM Server requires a token
secretToken: '',
// Set custom APM Server URL (default: http://localhost:8200)
serverUrl: '',
active: true,
}
```
- `package.json` dependencies:
Click to expand
```{
"name": "routeplanner-service",
"version": "1.0.0",
"description": "",
"module": "index.js",
"type": "module",
"scripts": {
"start": "node --experimental-modules --es-module-specifier-resolution=node -r elastic-apm-node/start ./index.js",
"debug": "node --experimental-modules --es-module-specifier-resolution=node --inspect-brk ./index.js"
},
...
}
```
Contributor guide
Assessment
This issue has not been assessed yet.