Setup Node.js Server and Environment variables in windows/Linux Easily

Shivam Gupta
2 min readNov 7, 2019

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.

Steps:

  1. Create a new project folder: test_node, add app.js file now npm init
  2. 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 -

app.js

Now we need to create pm2 configuration file with name: ecosystem.config.js

ecosystem.config.js

4. Now simply run by —

env: production/staging/development…

To check environment variable properly set see a project log by —

pm2 log

Now environment variable set successfully :)

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

Note

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!

--

--

Shivam Gupta

Full Stack Engineer (Web/App) working on different JS Technologies & frameworks— Angular, Node, Typescript, Ionic, Firebase, AWS, ElK...Love to write cool stuff