Enabling newrelic browser for node app is failing

I followed the instructions given for node.js agent to enable newrelic browser. But adding != newrelic.getBrowserTimingHeader() crashes the app giving internal server error.

I have added below code to my server.js .

if(process.env.NODE_ENV === "production") {
  var newrelic = require("newrelic");
  app.locals.newrelic = newrelic;

I am using node.js 0.10.40, express 3.1 and jade 0.35.

Hi @dilip

Thanks for providing those details up front. Would you be able to provide a more detailed code snippet for a reproduction and a stack trace for your error?

The first require statement must be for newrelic. Is the code you pasted above at the top of your server.js file? I’m not seeing the require for express, and I would be expecting to see that before setting app.locals.

The != denotes unescaped buffered code and is necessary for the Browser instrumentation to work properly. I’d like to make sure everything looks good in your setup before looking into the potential for a bug in our instrumentation.

With the requested items I will have a better insight to your setup and be able to provide better assistance.


Hi @seth
That code is not at the top. full server.js is as below

var express = require('express')
  , http = require('http')
  , path = require('path')
  , MySQL = require("./apps/models/mysql")
  , config = require("./config")(process.env.NODE_ENV);  

http.globalAgent.maxSockets = 100;

var app = express();

if(process.env.NODE_ENV === "production") {
    var newrelic = require("newrelic");   
    app.locals.newrelic = newrelic;

// Configuration
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use('/public', express.static(__dirname + "/public"));
  app.use('/dist', express.static(__dirname + "/dist"));

http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));

Hi @dilip

It is really important that newrelic is the first module required in your app in order for the agent to function correctly. Requiring other modules prior to newrelic could cause problems with our agent’s instrumentation.

Your conditional should be fine if put at the top of your server.js file, but you will need to move app.locals.newrelic = newrelic to somewhere after app = express().

if(process.env.NODE_ENV === "production") { 
  var newrelic = require("newrelic");

Another option is to put the conditional statement in the newrelic.js configuration. If you are toggling based on the value of an environment variable, you can do the following:

agent_enabled : process.env.NODE_ENV === 'production'

If you have a complex configuration toggling (multiple conditionals or the like) you can use something like the following at the end of your config file:

if (process.env.NODE_ENV === 'production') {
  module.exports.agent_enabled = true;
} else {
  module.exports.agent_enabled = false;

Please make this change, and if you are still experiencing the error, paste it into here.