Install Node.js and PM2
Published on: 5th Dec 2020
Updated on: 15th Oct 2023
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
There are two ways to install Node.js
-
To use
nvm
in managing Node.js version,curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash source ~/.bashrc
To view the latest version (please choose LTS - Long Term Support version),
nvm list-remote
nvm install v18.16.0 nvm use v18.16.0
Notes:
nvm
is not the only version manager. There is another one calledn
which will be explained below. -
To install Node.js directly.
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
(ornvm
as explained above) 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 (which rotate the log file automatically),
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
The daemon configuration file should be stored in the the following path or the value shown in 'Target path' after running the above command.
Target path: /etc/systemd/system/pm2-tester.service
-
16.Sep.2023: to remove pm2 as a daemon,
pm2 unstartup systemd
-
16.Sep.2023: to review the current pm2 settings,
pm2 report
-
16.Sep.2023: in case you are not sure if pm2 has been installed or not, run the following command to locate pm2.
whereis pm2
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
-
16.Sep.2023: to save the changes in pm2, we have to run the following command so that the app will be loaded after server has been reboot.
pm2 save
-
To view the app setting (the app name),
pm2 describe yourappname
-
For more information, visit the following page:
https://pm2.keymetrics.io/docs/usage/application-declaration/
Schedule a task
-
12.Jun.2023 - To run 'do-something.js' on an hourly basis:
-
create 'do-something.sh' file and saves it to
/usr/local/bin
directory#!/bin/bash # add the nodejs path so that it can be execute. PATH="/home/user1/.nvm/versions/node/v18.16.0/bin:$PATH" # run the script node /var/myapp/do-something.js
-
Edit the cron job,
sudo crontab -e
-
Add an entry to crontab,
0 * * * * /usr/local/bin/do-something.sh
-
Saves it by
CTRL+o
and press Y. -
Press
CTRL+x
to exit.
-
Troubleshooting
Error: "/usr/bin/env node: No such file of directory"
Updated on: 15.Oct.2023
If you are using nvm node version manager, use this comment to create a symlink:
sudo ln -s "$(which node)" /usr/bin/node
Error: "current user does not have permission to access the dev dir /root/.cache/node-gyp/xxx"
Updated on: 15.Oct.2023
When you run npm install better-sqlite3
and the following error returned,
prebuild-install warn install EACCES: permission denied, access '/root/.npm'
gyp WARN EACCES current user ("helomiao") does not have permission to access the dev dir "/root/.cache/node-gyp/18.16.0"
gyp WARN EACCES attempting to reinstall using temporary dev dir "/var/www/app-ciysys-com/demoapp/node_modules/better-sqlite3/.node-gyp"
Try to add --unsafe-perm
to install the package,
npm install better-sqlite3 --unsafe-perm
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.