Node.js - nodemon vs node - development vs production

nodemon actually reads the package.start value, so if you just set the start property to what you'd have in production, like node app.js, then run nodemon without any arguments, it'll run with package.start and restart as you'd expect in development.


Instead of putting logic in your "start", just add another script like "start-dev":"nodemon app.js" and execute it like "npm run-script start-dev".


You should be able to use NPM's start as a regular shell script.

"scripts": {
  "start": "if [$NODE_ENV == 'production']; then node app.js; else nodemon app.js; fi"
}

Now to start your server for production

$ NODE_ENV='production' npm start

or for development

$ NODE_ENV='development' npm start

I liked Daniel's solution, but thought it would be even cleaner to put it in a separate file, startup.sh:

#!/bin/sh

if [ "$NODE_ENV" = "production" ]; then
  node src/index.js;
else
  nodemon src/index.js;
fi

Then just change package.json to read:

"scripts": {
  "start": "../startup.sh"
},