question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: newrelic.setTransactionName is not a function

See original GitHub issue

Hello,

I’m trying to run newrelic on a TypeScript server which is rendering angular application (ng Universal).

Since angular 9 migration, we can’t use use newrelic anymore, because we got the error TypeError: newrelic.setTransactionName is not a function.

Description

Here’s a code sample:

// Other imports...
import * as express from 'express';
import * as newrelic from 'newrelic';

// Stuff...

server.get('/', (req, res, next) => {
    newrelic.setTransactionName('angular/home');
    angularHandler(req, res, next);
});
# Runtime error
TypeError: newrelic.setTransactionName is not a function
    at server.get (dist/server/us/main.js:10:7376209)
    at Layer.handle [as handle_request] dist/server/us/main.js:10:4081161)
    at next (dist/server/us/main.js:10:3811678)
    at Route.dispatch (dist/server/us/main.js:10:3811707)
    at Layer.handle [as handle_request] (dist/server/us/main.js:10:4081161)
    at dist/server/us/main.js:10:7850903
    at Function.process_params (dist/server/us/main.js:10:7852283)
    at next (dist/server/us/main.js:10:7850802)
    at module.exports (dist/server/us/main.js:10:6636313)
    at Layer.handle [as handle_request] (dist/server/us/main.js:10:4081161)

When I console.log(newrelic), I have the following object:

{
  config: {
      // ...
  }
}

Expected Behavior

setTransactionName is a function and works as expected.

Troubleshooting or NR Diag results

Your Environment

  • ex: Node version: v10.16.0
  • ex: Operating System and version: macOS High Sierra 10.13.6

Additional context

Looks the same issue as https://discuss.newrelic.com/t/requiring-newrelic-a-second-time-doesnt-return-an-instance-of-the-api-class/80527

But the discussed solutions doesn’t work for me

Changing import to import * as newrelic from 'newrelic/index'; and import newrelic from 'newrelic/index'; make build fails with errors

ERROR in ./node_modules/@newrelic/native-metrics/lib/pre-build.js
Module not found: Error: Can't resolve '../package' in '/Users/guillaume.lodi/vc-workspace/ngx-web/node_modules/@newrelic/native-metrics/lib'

ERROR in ./node_modules/node-gyp/lib/node-gyp.js
Module not found: Error: Can't resolve '../package' in '/Users/guillaume.lodi/vc-workspace/ngx-web/node_modules/node-gyp/lib'

ERROR in ./node_modules/@newrelic/native-metrics/lib/pre-build.js
Module not found: Error: Can't resolve 'aws-sdk' in '/Users/guillaume.lodi/vc-workspace/ngx-web/node_modules/@newrelic/native-metrics/lib'

Using require const newrelic = require('newrelic'); doesn’t work either.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
astormnewreliccommented, Sep 8, 2020

Hi @doliG, nice to meet you. This sounds like both a typescript configuration issue and possibly a webpack issue. I’m going to describe what I see and ask you a few questions in the hopes of getting to the bottom of this.

It looks like we have two issues here – both come down to how angular is compiling your typescript and javascript code.

First – it sounds like something in your or angular’s TypeScript compiler chain is trying to resolve NodeJS modules for you and incorrectly loading the New Relic newrelic.js configuration file instead of the newrelic module in node_modules

We couldn’t reproduce this specific error when we ran through the angular universal tutorial. Do you, by chance, have the NestJS CLI installed (or have a basic example where we could reproduce the problem?) We’ve seen this “resolves to the configuration file instead of the agent” problem before with NestJS and have an issue opened with them to fix it.

Also, it sounds like using the import * as newrelic from 'newrelic/index'; work around solved the “loads newrelic.js file instead of the agent” problem, but left you with another one.

The second problem are these errors

ERROR in ./node_modules/@newrelic/native-metrics/lib/pre-build.js
Module not found: Error: Can't resolve '../package' in '/Users/guillaume.lodi/vc-workspace/ngx-web/node_modules/@newrelic/native-metrics/lib'

ERROR in ./node_modules/node-gyp/lib/node-gyp.js
Module not found: Error: Can't resolve '../package' in '/Users/guillaume.lodi/vc-workspace/ngx-web/node_modules/node-gyp/lib'

ERROR in ./node_modules/@newrelic/native-metrics/lib/pre-build.js
Module not found: Error: Can't resolve 'aws-sdk' in '/Users/guillaume.lodi/vc-workspace/ngx-web/node_modules/@newrelic/native-metrics/lib'

Do you happen to know if Angular Universal uses webpack under the hood? We’re asking because these errors look a lot like errors we’ve seen where webpack tries to compile your entire node_modules folders for a server side application, including some Node Utility scripts. We usually recommend folks give the webpack-node-externals module a try to work around these sorts of issues. We even have an experimental webpack plugin that wraps webpack-node-externals for you.

If you can get us answers to those questions we’ll be better able to better tell what’s going on.

0reactions
michaelgoincommented, Jul 8, 2021

Closing this as it falls under a general webpack support. Currently, we don’t have plans to add better support for webpack (but that is subject to change as tracked as a part of webpack feature requests).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Requiring 'newrelic' a second time doesn't return an ...
Just to be clear, I'm getting newrelic.setTransactionName is not a function. Halp plz. philweber August 9, 2019, 8:33am #2.
Read more >
[Node] newralic.noticeError is not a function
Hi i am getting error TypeError: Newrelic.noticeError is not a function Please help me how can i fix it Thanks.
Read more >
New Relic requires you name this application - Agents
[Java] Use setTransactionName causes error: New Relic requires you name this application · Support: Full-Stack Observability (FSO) Agents.
Read more >
Agents - New Relic Explorers Hub
using() . We pass along the returned value from the function so returning from Promise.try() should still work fine. The only thing is,...
Read more >
Node.js Agent API can not be used
This is an error that occurs. var transaction = newrelic.createWebTransaction('/', function(){ ^ TypeError: newrelic.createWebTransaction is not ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found