Nodejs APM - Can't resolve 'async_hooks'

Hello,

We are trying to use APM for our React nodejs app.

We have added the dependency in our package.json to install the elastic-apm-node module:

"elastic-apm-node": "^3.31.0"

And then we added the below line in our index.js after our imports, but before the start of any other processes:

var apm = require('elastic-apm-node').start()

We get the following error messages we try to run an install of our project:

./node_modules/elastic-apm-node/lib/instrumentation/span.js
Module not found: Can't resolve 'async_hooks' in '\node_modules\elastic-apm-node\lib\instrumentation'

Any ideas what we are doing wrong?

Thanks!

Hey Joseph, nice to meet you.

The most common reason for the error you're seeing

./node_modules/elastic-apm-node/lib/instrumentation/span.js
Module not found: Can't resolve 'async_hooks' in '\node_modules\elastic-apm-node\lib\instrumentation'

is a frontend build system trying to resolve the built-in Node.js async_hooks module. You didn't mention it, but I'm guessing that index.js is your root React component?

The Node.js agent will allow you to monitor backend Node.js services and Node.js CLI applications. It will not allow you to monitor a React application, or any other frontend framework. To monitor a React application, you'll want to use the "Real User Monitoring JavaScript agent" (sometimes called the browser agent).

So, if what I've said above about your index.js file is correct, the steps I'd take to fix your problem are

  1. Remove the Node Agent from your index.js file
  2. Add the RUM React integration to your project
  3. If you have a separate backend service written in Node.js, add the Node.js agent to its initial file

Does that make sense/get your moving in the right direction?

Hi Alan,

That makes perfect sense, thank you!

I do have one other question now that we will be going the RUM route. Is there auto-instrumentation if we just add the agent to our project by following the "Using Bundlers" section in the following section:

Or will we not get any useful metrics unless we integrate with React using the Integration instructions that you posted? We are trying to limit the code changes as much as possible. We loved the way that the java agents require no code change as we like to keep APM separate from the code as much as we can.

Thank you!

Hi @jsoule6,

Thanks for your interest in RUM.

The RUM agent provides with auto instrumentation for:

  1. page load metrics
  2. User interactions (clicks)
  3. Route changes
  4. Fetch and xhr requests in order to create http-request transactions
  5. Errors are also automatically observed

This last link gives you a overall view of available instrumentations. There you will also see that you can disable them in case you need it.

Does that help you?

Thanks,
Alberto

Thank you @Alberto_Delgado. We are going to give this a try without customizing any routes and see how it goes.

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.