Install Node.js and PM2
Published on: 5th Dec 2020
Updated on: 30th Jan 2022
What is Node.js
Node.js is a virtual machine for hosting JavaScript programs. It is powerful such that you can write a simple website in a few lines of code.
Why we need it
You may develop your website using Node.js or develop a script to analyze the web access log, etc.
Here's how you install Node.js
-
To install Node.js
cd ~ # download the setup script - you may review it before executing this script curl -sL https://deb.nodesource.com/setup_16.x -o nodesource_setup.sh sudo bash nodesource_setup.sh sudo apt install nodejs sudo apt install build-essential
-
To check version,
node -v npm -v
-
To remove Node.js
sudo apt-get purge --auto-remove nodejs
-
If you install Node.js directly, you will have more work when upgrading it to the latest version. To avoid such a situation, we are going to install
n
which is a Node.js version manager. To installn
, run the following command,# create a directory for 'n' program. sudo mkdir -p /usr/local/n sudo chown -R $(whoami) /usr/local/n sudo mkdir -p /usr/local/node_modules sudo chown -R $(whoami) /usr/local/node_modules # create the necessary directories for 'n' to run sudo mkdir -p /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share sudo chown -R $(whoami) /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share
-
If you are running Nodes.js on any of the Linux variant, you may install
n
which is a Node.js version manager.# ready to install `n` package npm install -g n
-
To view the available Node.js version to install,
n --lts
-
To install the latest long term support (lts) version,
n lts
-
To install a specific version,
n 17.4.0
-
-
If you are running Node.js on Windows, you will have to install
nvm
. Please following the installation steps in the following page:
Maintaining Node.js app dependencies
First, you have to set the current location to the same as the package.json
of your app.
cd /var/www/app1
-
To check if there is any outdated packages
npm outdated
-
To update the
node-cron
package (for example) to the latest version, run the following command:npm install node-cron@latest
Install PM2
The website hosted in Node.js will not be back online after server reboot. In this case, we need a program to start the website. There are many options to achieve this. For us, we choose pm2
due to its flexibility and easy to configure.
-
To install PM2
npm install pm2@latest -g
-
To install log rotate
pm2 install pm2-logrotate
-
Set as startup (in Linux) - run the following command in a NON root context,
pm2 startup systemd
-
Then, the following result will appear on the screen. You need to copy the command on the screen and run it accordingly. Please take note that 'tester' is our user ID and you may expect different user ID and path.
sudo env PATH=$PATH:/usr/local/bin /usr/local/lib/node_modules/pm2/bin/pm2 startup systemd -u tester --hp /home/tester
-
To add your Node.js website to
pm2
# goto the app directory cd /var/www/app1 # start the Node.js app pm2 start index.js # save the configuration pm2 save
-
To view the app setting (where 'index' is the app name),
pm2 describe index
Adding Node.js app to pm2 using ecosystem.config.js file
-
This can be done easily. First, you have to create a
ecosystem.config.js
file in the app directory (same as the startup script location which is/var/www/app1
used in our blog post) which has the following contents:module.exports = { apps: [ { name: "yourappname", script: "./index.js", exec_mode: "cluster", instances: 2, shutdown_with_message: true, env: { NODE_ENV: "production", } }]};
-
The above settings
name
- the app name that will be displayed when you run the command ofpm2 ls
.script
- it is the script file to be running.exec_mode
- it can be (1) running your app infork
mode or (2)cluster
which runs your app multiple instances with port sharing. Default isfork
mode.instances
- the number of instances to be started bypm2
.shutdown_with_message
- if true, the app will be shutdown withprocess.send('shutdown')
instead ofprocess.kill(pid, SIGINT)
. With true value, your app may listen to the 'shutdown' app message.
-
After that, run the following command to add the app to
pm2
,pm2 start ecosystem.config.js
-
For more information, visit the following page:
https://pm2.keymetrics.io/docs/usage/application-declaration/
References
-
For Node.js, visit this page: https://nodejs.org/en/
-
For
n
, Node.js version manager (for Linux platform), visit this page: https://github.com/tj/n -
For
nvm
(for Windows platform), visit this page: https://github.com/coreybutler/nvm-windows -
For
pm2
, you may find out more information at https://pm2.keymetrics.io/
Related posts
- For how to allow public to access your Node.js website, please refers to Install Nginx
Jump to #UBUNTU blog
Author
Lau Hon Wan, software developer.