TypeError: newrelic.setTransactionName is not a function
See original GitHub issueHello,
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:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
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 thenewrelic
module innode_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 “loadsnewrelic.js
file instead of the agent” problem, but left you with another one.The second problem are these errors
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 wrapswebpack-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.
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).