Nodejs app with npm start script

user6147402 picture user6147402 · Jul 26, 2016 · Viewed 13.9k times · Source

I'm very new to nodejs.

In my dockerized environment, I want to provide appdynamics support to nodejs apps. This mandates every app to require the following as the first line in their app.

require("appdynamics").profile({
    controllerHostName: '<controller host name>',
      controllerPort: <controller port number>, 
      controllerSslEnabled: false,  // Set to true if controllerPort is SSL
      accountName: '<AppDynamics_account_name>',
      accountAccessKey: '<AppDynamics_account_key>', //required
      applicationName: 'your_app_name',
      tierName: 'choose_a_tier_name', 
      nodeName: 'choose_a_node_name', 
 });

I plan to do that by providing a wrapper called appdynamics.js around the app's entry file. Details:

  1. I run a script in my nodejs docker image to replace the entry file name in the app's package.json with "appdynamics.js", where appdynamics.js has the above appdynamics related require statement. Ex : {scripts { "start" : "node server.js" }} will be replaced with {scripts { "start" : "node appdynamics.js"}}

  2. Then, i "require" the "server.js" inside appdynamics.js.

  3. Invoke npm start.

My only concern is this:

If the package.json had something like scripts { "start" : "coffee server.coffee" }, my script will replace it to { "start" : "coffee appdynamics.js" }. and then my script will invoke npm start, which will error out.

What is the best way to solve this?

This is a follow up question to Use "coffee" instead of "node" command in production

Answer

Tomasz Jakub Rup picture Tomasz Jakub Rup · Jul 28, 2016
  • Write a wrapper called appdynamics.coffee
  • Compile this wrapper to .js
  • Replace server.js with appdynamics.js and server.coffee with appdynamics.coffee

After this operations

{
  "scripts": {
    "start": "node server.js"
  }
}

will be

{
  "scripts": {
    "start": "node appdynamics.js"
  }
}

and

{
  "scripts": {
    "start": "coffee server.coffee"
  }
}

will be

{
  "scripts": {
    "start": "coffee appdynamics.coffee"
  }
}