Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to start typescript app with APM agent. #3831

Open
2 tasks
khteh opened this issue Jan 15, 2024 · 1 comment
Open
2 tasks

Failed to start typescript app with APM agent. #3831

khteh opened this issue Jan 15, 2024 · 1 comment
Labels
agent-nodejs Make available for APM Agents project planning. awaiting reply community

Comments

@khteh
Copy link

khteh commented Jan 15, 2024

Describe the bug

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

$ n start

> node.jsrestapi@1.0.0 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
@github-actions github-actions bot added agent-nodejs Make available for APM Agents project planning. community triage labels Jan 15, 2024
@trentm
Copy link
Member

trentm commented Jan 15, 2024

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agent-nodejs Make available for APM Agents project planning. awaiting reply community
Projects
None yet
Development

No branches or pull requests

2 participants