[Node] js agent and puppeteer background transaction throws error

We want to use background transaction to instrument background PDF report generation with puppeteer. It throws an error:

TypeError: Cannot redefine property: __NR_segment

Please provide information on your environment, and any further information you believe is relevant, such as the below:

  • APM Agent Language: Node.js
  • APM Agent Version: 6.1.0
  • Operating System: Ubuntu
  • Operating System Version: 16.04
  • Frameworks your app is using: ExpressJS

This code throws the error:

nr.startBackgroundTransaction(nrTransactionName, async function backgroundTransactionhandler() {
  const nrTransaction = nr.getTransaction()

  let browser
  try {
    browser = await puppeteer.launch({
      headless: true,
      args: [
/* ... */

This code is a part of background worker. It processes requests from a queue.

Hello @animirr :wave:

Could you share a link to the application in New Relic One? Only those on your account and New Relic Admins will be able to view any app details.

The __NR_segment is a property the Node agent sets on objects during instrumentation. The TypeError you shared suggests the something is making this object immutable.

Do you have any other Node Agent custom instrumentation in you application and does it produce a similar error? Is this exclusive to .startBackgroundTransaction?

Are you using Typescript, CoffeeScript or any other variations of Javascript? Are you transpiling your code, for example using Babel?

Are you using any other application monitoring products in your application?

Looking forward to your response!

@lindseybaker Hi, It has been a while :slight_smile:

New Relic One link. I don’t think, any useful data got there because of error.

We don’t see similar error in other cases, like: .startSegment or .startWebTransaction. It was the first time we tried .startBackgroundTransaction.

We use Babel.

Not I could think of as interferer.

Note, puppeteer also opens a page on our application. I suspect, that puppeteer launched inside Node.js tries to initiate underlying newrelic session, but it is already in newrelic session.

Hello @animirr, welcome back!

I have two things I would like to rule out as possible issues:

  1. The newrelic has to be loaded before the modules that is instruments. Babel can potentially rearrange this load order. To prevent this you can use the -r require flag to preload the newrelic module at startup using the Node command line option -r.

    For example, your start up script would look like: node -r newrelic ./dist/server.js

    Once you have preloaded the newrelic module, you will then still need to call the module at the top of your main file with require('newrelic'); or import newrelic from 'newrelic'.

  2. We would like to make sure that .startBackgroundTransaction isn’t trying to start a transaction when there is already an existing transaction object. We can test this in two ways. Either:

  • Remove the custom instrumentation then log nr.getTransaction(), this will return the current transaction object. If no object is returned, we can rule this out as a potential issue. If an object is returned, then you will need to use an alternate API method to instrument this segment which is also the second way to test if a transaction object already exist, described below:

  • Replace startBackgroundTransaction with .startSegment. If this segment is reported to New Relic, then a transaction object already exists and the handler function is best instrumented using this method. Configure .startSegment to be recorded so that you can easily find the metric reported for this segment:


Once we can eliminate the above as possible problems, the next steps will be to create a minimal reproduction for our Node Agent development team to test. We have a similar issue being tracked in Github Issues but have not been able to reproduce the same error yet. It would be great if you could either add your case to the existing issue:

If you need further help on any of the above, please let me know!

1 Like