Let’s install it globally. Open terminal of your choice and run the below command in any directory.
npm install pm2 -g
Hosting Your NodeJs App
Hosting your app with PM2 is as simple as ABC. Open any terminal, navigate to your project’s root directory and run the below command.
pm2 start <YourStartUpFileName>.js
That’s it. Your app is now hosted and is accessible on the port specified.
Other Useful PM2 Commands
1. PM2 list
The above command will list all the processes registered at PM2.
2. PM2 stop
The above command will stop the specified process. You can specify the process by either process id or process name.
pm2 stop app.js // (By Process Name - Works in the project directory) pm2 stop 0 // (0 is process id - Works everywhere)
3. PM2 delete
Deletes the specified process.
4. PM2 restart
Restarts the specified process.
5. PM2 monit
Monitors resource usage of applications.
Running PM2 on Windows Startup
- npm install pm2-windows-startup -g
- pm2-startup install
For saving the current list of processes, run below command
Now, on every window restart, your saved list of processes will be running again. Make sure you run save command after adding new processes.
Impressed? PM2 Plus has a lot more to offer. A Web based fancy dashboard for real time monitoring, metrics, notifications, issue tracking, wait wait, Why I am explaining all this, Go see yourself!