Node.js - nodemon vs node - development vs production

22.8k views Asked by At

I would like to use $>npm start and have it use "nodemon" for development and "node" for production. I can't put conditional logic in my package.json file, so how is this best accomplished?

4

There are 4 answers

2
Remy Sharp On BEST ANSWER

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.

2
Erfa On

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"
},
0
Yash Ranjan On

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".

4
Daniel On

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