[ nodejs ] pm2 : node package restart tool

Installation

npm i -g pm2
npm i -g babel-cli ( for --intepreter babel-node option )

run 

Example:

node -r dotenv/config ./node_modules/.bin/react-scripts start dotenv_config_path=.env.production

in pm2

pm2 start --node-args="-r dotenv/config" node_modules/react-scripts/bin/react-scripts.js --name app --log-date-format 'YYYY-MM-DD HH:mm:ss.SSS' -- start dotenv_config_path=.env.$NODE_ENV

Example2:

NODE_ENV=development node ./dist/server.js

in pm2

NODE_ENV=$NODE_ENV pm2 start --name api --log-date-format 'YYYY-MM-DD HH:mm:ss.SSS' ./dist/server.js

Example3:

nodemon ./src/server.js --exec babel-node

in pm2

NODE_ENV=$NODE_ENV pm2 start --name crontab --log-date-format 'YYYY-MM-DD HH:mm:ss.SSS' --interpreter babel-node ./src/server.js

list all apps

pm2 list

kill all apps

pm2 kill

View log in real-time

pm2 logs api

[ react-scripts ] deployment

package.json

"start": "react-scripts start",
"start:production": "node -r dotenv/config ./node_modules/.bin/react-scripts start dotenv_config_path=.env.production",
"build": "react-scripts build",
"build:development": "node -r dotenv/config ./node_modules/.bin/react-scripts build dotenv_config_path=.env.development"

NOTE :

  • default `react-scripts start` reads .env.development
  • default `react-scripts build` reads .env.production

use serve as a static server ( production )

react-scripts build
yarn global add serve
serve -s build -p 3000

which serve

yarn global bin
/home/pong/.yarn/bin

ref : https://github.com/tuchk4/react-app-env/blob/master/README.md