Your data. Anywhere you go.

New Relic for iOS or Android

Download on the App Store    Android App on Google play

New Relic Insights App for iOS

Download on the App Store

Learn more

Close icon

Require 'newrelic' in setImmediate callback


Hi! I’m using newrelic in Node.JS app. My hosting plan does not provide clustering so on deploy there is a downtime for users during my app restart. I’m trying to reduce that downtime: I measured modules load duration and found that ‘newrelic’ has the highest load time ~ 200ms.
Is it safe to require ‘newrelic’ in setImmediate() callback instead of first line of entry script?


Hey @noginsk!

Great questions! If I’m understanding you correctly, rephrased they could be…

  1. Will the New Relic module work with a callback?
    Deferring the loading of the agent (e.g. putting the require in a callback) will also delay the insertion of the module loading hooks used for instrumentation. This is effectively the same as not requiring newrelic at the top of the file, and will open the user up to missing instrumentation. Doing this is not advised.

  2. Do we have a way to reduce module load time?
    We try to get out of the way of start up as much as possible. You can try to adjust to not use a config file and only use environment variables, but we suspect the gains would be negligible. Another source of some start up overhead is the native metrics module; if you aren’t using that data you can turn it off and may see a small difference during start up.

Based on what you’ve shared so far this is what we can tell you. If this doesn’t help please give us a little more context into your use case, environment setup and configuration.

All the best!


Hi michaelbrooks!

Thanks for the reply. So moving newrelic to callback is not advised, but as I understand is possible. A little more context of my case:
I want to start HTTP server as fast as possible, and all other stuff already moved into callback. I have a function like this:

function startServer(callback) {
  const http = require('http');
  const server = http.createServer((req, res) => {
    res.end('Hello world');
  server.listen(8000, callback);

I understand that advisable way is following:

startServer(() => {
  console.log('server started');

The question is - will my server be instrumented correctly, if I require newrelic inside a callback? E.g.:

startServer(() => {
  console.log('server started');

Thanks in advance!



No it will not be instrumented correctly if required inside a callback.

Any work arounds will not be supported by our Agent team for troubleshooting and would likely break with any future updates as the Agent is not designed to work within a callback.

I hope this helps clarify.


Ok! Thanks for your reply.