Nodejs PM2 and Forever

What is forever?

forever is a node.js package that is used to keep the server alive even when the server crash/stops. When the node server is stopped because of some error, exception, etc. forever automatically restarts it.

The purpose of Forever is to keep a child process (such as your node.js web server) running continuously and automatically restart it when it exits unexpectedly and it will error log also.

From the npmjs https://www.npmjs.com/package/forever

A simple CLI tool for ensuring that a given node script runs continuously (i.e. forever)

Forever can be used as

forever start app.js

It provides many useful functions which you can see in the link above.

 

Install forever: npm install forever

Install forever globally: sudo npm install forever -g

 

For forever more commands: https://github.com/foreverjs/forever/

 

What is PM2?

PM2 is a process manager for Node.js applications. It’s written in Node.js (JavaScript) itself and Shell. The utility also integrates a load balancer. You can keep application server processes alive and reload/restart them with zero downtime.

PM2 has a rich set of functionalities and we just pointed out some of the features. There are a lot other process managers for Node.js applications like supervisor, forever, etc. We think PM2 has some major advantages over all others: intuitive API accessible via CLI, stability, great console output. That later may not be one of the critical requirements when deciding for a tool to use. Nevertheless, we had multiple situations where PM2’s output saved us a lot headaches and time when searching for process information

The list below outlines functionality which we think is highly beneficial for every system administrator running Node.js apps in production.

 

Major Feature Set

  • Process management including automatic app restarts on failure or system reboots
  • Application monitoring
  • Declarative configuration via JSON file
  • Log management
  • Built-in cluster mode
  • Startup script generation for *nix systems
  • Seamless updates
  • Integrated module system

To run PM2: pm2 start app.js

Install forever: npm install pm2

Install forever globally: sudo npm install pm2 -g

 

For PM2 more commands: https://github.com/tes/PM2

Compare pm2 and Forever

NPM vs YARN

 

npm and Yarn are two well-known JavaScript package managers. If you’re not familiar with what a package manager does, it essentially is a way automate the process of installing, updating, configuring, and removing pieces of software (AKA packages) retrieved from a global registry. In this post, we’ll be going over what differences exist between two of the most popular JavaScript package managers – npm and Yarn.

 

What is npm?

The JavaScript node package manager, typically abbreviated in all lowercase as npm, is the default method for managing packages in the Node.js runtime environment. It relies upon a command line client and a database made up of public and premium packages known as the the npm registry. Users can access the registry via the client and browse the many packages available through the npm website. Both npm and its registry are managed by npm, Inc

 

What is yarn?

Yarn was developed by Facebook in attempt to resolve some of npm’s shortcomings. Yarn isn’t technically a replacement for npm since it relies on modules from the npm registry. Think of Yarn as a new installer that still relies upon the same npm structure. The registry itself hasn’t changed, but the installation method is different. Since Yarn gives you access to the same packages as npm, moving from npm to Yarn doesn’t require you to make any changes to your workflow.

 

To install npm in ubuntu: https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04

To install yarn in ubuntu: https://yarnpkg.com/lang/en/docs/install/#debian-stable

 

Common NPM commands in Yarn

PM2 commands:

 

Start an app-link

$ pm2 start app.js

 

Start and auto-restart on file change-link

$ pm2 start app.js –watch [–ignore-watch /*/]

 

List apps-link

pm2 list

 

Show extended info-link

$ pm2 show <app_name>

Restart-link

$ pm2 restart app

 

Restart and update env-link

$ NODE_ENV=production pm2 restart app –update-env

 

Stop-link

$ pm2 stop app

 

Delete-link

$ pm2 delete app

 

Show logs link

$ pm2 logs

 

Show env link

$ pm2 env <pm_id>

 

Start PM2 at boot-link

$ pm2 startup

 

Set a name-link

$ pm2 start app.js –name=”name”

# or update name

$ pm2 restart app –name=”new-name”

 

Reset Restart Counters link

$ pm2 reset all

 

Monitoring-link

$ pm2 monitor

 

Dump all process data-link

$ pm2 prettylist

# or

$ pm2 show <pm_id|app_name>