Node.js - nodemon vs node - development versus production

I would like to use $>npm start and use "nodemon" and "node" to develop it. I cannot put conditional logic in the package.json file, so how best to do this?

+12
source share
4 answers

nodemon actually reads the value of package.start , so if you just set the start property to what you would have in production, for example node app.js , then run nodemon without any arguments, it will work with package.start and restart as you would expect in development.

+5
source

You can use NPM startup as a regular shell script.

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

Now, to start the server for production

 $ NODE_ENV='production' npm start 

or for development

 $ NODE_ENV='development' npm start 
+6
source

I liked Daniel's solution, but I 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:

 "scripts": { "start": "../startup.sh" }, 
+5
source

Instead of entering the logic into your “start”, simply add another script, for example “start-dev”: “nodemon app.js”, and execute it as “npm run-script start-dev”.

+1
source

Source: https://habr.com/ru/post/977908/


All Articles