user2066880
user2066880

Reputation: 5034

Node.js: Switch npm script based on context

Is there a way to configure package.json to run a different npm start script based on context? For example, I would like to run DEBUG=http nodemon app.js when I am developing. But, I would like to run node app.js on production.

Upvotes: 0

Views: 114

Answers (1)

medokin
medokin

Reputation: 620

Create a new file (e.g. server.js) and insert your app.js content.

Use this code sample inside of app.js

var sys = require('sys')
var exec = require('child_process').exec;
function puts(error, stdout, stderr) { sys.puts(stdout) }


var isDev = // Check if on dev machine

if(isDev){
  exec("DEBUG=http nodemon server.js", puts);
} else {
  exec("node server.js", puts);
}

Upvotes: 1

Related Questions