Back to home

Node Version Manager (NVM)

This article gives a short brief on how to use node.js in your userspace with individual versions.

With “Node Version Manager” (short ‘nvm’), a tool available on Github by the user creationix. You can install your own nodejs versions and even manage them. (it’s also possible to install multiple versions.

Info: the newest version of nvm can be found under https://github.com/creationix/nvm

Installation of nvm

The easiest way to install nvm, would be over the install script, which you can get using CURL or Wget:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash

or

wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash

This way nvm gets installed directly and it even exports in your bashrc.
To use nvm direct from the CLI, it has to be reloaded:

source .bashrc

optional:
If you wish bash Completion for nvm, you can add the following string at the end of of your .bashrc and don’t forget to reload .bashrc using source:

echo "[[ -r $NVM_DIR/bash_completion ]] && . $NVM_DIR/bash_completion  #adds bash completion for nvm" >> ~/.bashrc 

source .bashrc

With nvm, you know have a working versions Manager.

Usage of NVM

here you have a short overview of the most important commands:

  • check available version:
nvm ls-remote
  • check installed versions:
nvm ls
  • install an arbitrary version :
nvm install 5.0
  • use an arbitrary version
nvm use 5.0
  • one time start of a certain (already installed) Version:
 nvm run 4.2 --version
  • use iojs with the help of nvm: nvm install iojs

Process manager

When using nvm, it can be utile to also use a process manager. for example pm2

PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.

You must have npm installed in order to use pm2 and it’s features.

with the following command you can install pm2:

npm install pm2 -g

Usage of pm2

  • simple activation of applikations:
pm2 start app.js
  • Start multiple instances of application in cluster mode:
pm2 start app.js -i 4
  • update pm2 for new functions:
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 informations 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

Systemd user unit

In order to startup pm2 as a user service, a systemd user unit file (~/.config/systemd/user/pm2.service) like the following could be used:

[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/

[Service]
Type=forking
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/home/www-data/.nvm/versions/node/v8.5.0/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/home/www-data/.pm2
PIDFile=/home/www-data/.pm2/pm2.pid

ExecStart=/home/www-data/.nvm/versions/node/v8.5.0/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/home/www-data/.nvm/versions/node/v8.5.0/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/home/www-data/.nvm/versions/node/v8.5.0/lib/node_modules/pm2/bin/pm2 kill

[Install]
WantedBy=default.target

Didn't find what you were looking for?

Contact our support:

+41 44 637 40 40 Support Portal support@nine.ch