apm-agent-nodejs icon indicating copy to clipboard operation
apm-agent-nodejs copied to clipboard

Failed to start typescript app with APM agent.

Open khteh opened this issue 2 years ago • 1 comments

Describe the bug

Putting import 'elastic-apm-node/start' as the first line of startup code in Typescript app results in

$ n start

> [email protected] start
> export NODE_ENV=development&& node --experimental-specifier-resolution=node build/src/webapi/server.js

node:internal/process/esm_loader:34
      internalBinding('errors').triggerUncaughtException(
                                ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/usr/src/Node.JSRestAPI/node_modules/elastic-apm-node/start' imported from /usr/src/Node.JSRestAPI/build/src/webapi/server.js
Did you mean to import elastic-apm-node/start.js?
    at finalizeResolution (node:internal/modules/esm/resolve:263:11)
    at moduleResolve (node:internal/modules/esm/resolve:908:10)
    at defaultResolve (node:internal/modules/esm/resolve:1121:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:396:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:365:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:240:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:85:39)
    at link (node:internal/modules/esm/module_job:84:36) {
  code: 'ERR_MODULE_NOT_FOUND',
  url: 'file:///usr/src/Node.JSRestAPI/node_modules/elastic-apm-node/start'
}

Node.js v21.5.0

To Reproduce

Steps to reproduce the behavior:

  1. Use this config '...'
  2. Then call '....'
  3. Then do '....'
  4. See error

Expected behavior

Environment (please complete the following information)

  • OS: [e.g. Linux] Ubuntu 23.10
  • Node.js version: 21.5.0
  • APM Server version:
  • Agent version: 4.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
replace this line with your agent config options

package.json dependencies:

Click to expand
replace this line with your dependencies section from package.json

khteh avatar Jan 15 '24 07:01 khteh

@khteh Hi. The error message says:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/usr/src/Node.JSRestAPI/node_modules/elastic-apm-node/start' imported from /usr/src/Node.JSRestAPI/build/src/webapi/server.js
Did you mean to import elastic-apm-node/start.js?

Could you try changing your import line to:

import 'elastic-apm-node/start.js'

I'm not familiar with the --experimental-specifier-resolution=node Node.js CLI option, so I don't know if that could be involved.


Are you able to show your "tsconfig.json" and enough of build/src/webapi/server.js to see what the actual executed JavaScript looks like?

From the stacktrace at finalizeResolution (node:internal/modules/esm/resolve:263:11) it looks like Node.js is using the ESM module loader, so I am guessing that you are using ES modules -- i.e. you have "type": "module" in your package.json. If so, you'll want to read https://www.elastic.co/guide/en/apm/agent/nodejs/current/esm.html to understand the APM agent's support for ES Modules. As a start, you'll need to use the --experimental-loader=elastic-apm-node/loader.mjs CLI option for the APM agent to be able to automatically instrument modules.

trentm avatar Jan 15 '24 22:01 trentm