Setup Node.js Server and Environment variables in windows/Linux Easily
Many times I notice setting an environment variable in Windows/Linux not work quickly so there is a package manager tool pm2 use for managing env variables and setup server
PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without the downtime and to facilitate common system admin tasks.
- Create a new project folder: test_node, add app.js file now npm init
- Install pm2 in your Linux/ Windows machine by npm:
npm install pm2 -g
3. To create server install express: npm i express
3. Add app.js file. The project looks like -
Now we need to create pm2 configuration file with name: ecosystem.config.js
4. Now simply run by —
To check environment variable properly set see a project log by —
Important pm2 commands
1. pm2 list : TO see all running app/processes
2. pm2 restart 0 : To restart server/ process. Here 0: id
3. pm2 kill: to kill all process
4. pm2 log: check logs
5. pm2 flush: To clean previous logs
pm2 installed globally to any machine windows/Linux.
In Linux running instance (server)which runs in background called: process,
In windows called Daemon Process
Please write in the comment section for any question or suggestion. Give a clap if like this Article. Thanks!