Run Node.js Applications
This article describes how to run a Node.js installation in userspace.
The installation in userspace can be recommended for several reasons:
- It gives you control over which Node.js version you use.
- You can use different versions for different applications.
- It allows you to install "global" npm packages.
Using the "Node Version Manager", hereafter called nvm
, you can install and manage multiple Node.js versions.
The current version of nvm
can be found at github.com/creationix/nvm.
Installation of nvm
nvm
is installed via an installation script, which can be retrieved from Github with curl
:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
This will install nvm directly and export it to your .bashrc
.
To be able to use nvm
directly via the CLI, the .bashrc
must be read in again:
source .bashrc
Usage of NVM
Show available version
nvm ls-remote
Check installed versions
nvm ls
Installing a version
Install the latest LTS version
nvm install 'lts/*'
Install a specific version
nvm install 5.0
Use a specific version
nvm alias default 'lts/*'
Use an arbitrary version in the current session
nvm use 5.0
Run a command with a specific version
# nvm exec [<version>] [<command>]
# Run <command> on <version>. Uses .nvmrc if available and version is omitted.
nvm exec 5.0 node --version
Configure a Node.js version for a specific project
This can be done using a .nvmrc file:
$ cat .nvmrc
lts/*
Manage Applications with Process Manager (pm2)
pm2
is a process manager for Node.js applications with an integrated load balancer. Among other things, this enables you to
run applications permanently, reload applications without downtime and simplify common administration tasks.
You can install pm2
with the following command:
npm install pm2 -g
Usage of pm2
Start applications
pm2 start app.js
Start multiple instances of an application in cluster mode
pm2 start app.js -i 4
Update pm2
pm2 update
Process Monitoring
pm2 list # List all processes started with PM2
pm2 monit # Display memory and cpu usage of each app
pm2 show [app-name] # Show all information about application
Startup/Boot management
pm2 startup # Detect init system, generate and configure pm2 boot on startup
pm2 save # Save current process list
pm2 resurrect # Restore previously save processes
pm2 unstartup # Disable and remove startup system
Start Application after Reboot with Systemd
To start pm2
as a user service when the system is restarted, a systemd unit (~/.config/systemd/user/pm2.service
) can be used:
[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
[Service]
Type=forking
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=NODE_ENV=production
Environment=NODE_VERSION=14
Environment=PM2_HOME=%h/.pm2
PIDFile=%h/.pm2/%p.pid
ExecStart=%h/.nvm/nvm-exec pm2 resurrect
ExecReload=%h/.nvm/nvm-exec pm2 reload all
ExecStop=%h/.nvm/nvm-exec pm2 kill
[Install]
WantedBy=default.target
More information about how to use SystemD is available in the following support article.