Node.js is an open source cross-platform JavaScript run-time environment built on Chrome’s JavaScript engine that allows server-side execution of JavaScript code. It is mainly used to build server-side applications, but it is also very popular as a full-stack and front-end solution. NPM is the default package manager for Node.js and the world’s largest software registry.

In this tutorial, I’m going to explain how to install Node.js and NPM on Raspberry Pi. I’m going to assume that you have Raspbian installed on your Raspberry Pi.

Install Node.js and NPM using NVM

NVM (Node Version Manager) is a bash script that allows you to install and manage multiple Node.js versions. Use this method if you need to install a specific Node.js version or if you need to have more than one Node.js versions installed on your Raspberry Pi.

To install NVM run the following curl command which will download and run the nvm installation script:

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

The installation script will clone the NVM repository from Github to the ~/.nvm directory and add the NVM path to your Bash profile.

Close and reopen your terminal to start using NVM, or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

As the output says, you can either open a new shell session or run those commands that will add the nvm path to your current session.

Do whatever is easier for you.

To ensure that nvm is properly installed type:

nvm --version

Assuming you see a version number, you can now install the latest available version of Node.js by running:

nvm install node

Once NVM has downloaded Node and made it available to you, it can be verified by:

node --version

To better demonstrate how NVM works we’ll install two more versions, the latest LTS version and version 10.

nvm install --lts
nvm install 8.9.4

Once both versions are installed, list the Node.js instances by typing:

nvm ls
->       v8.9.4
       v10.16.3
        v12.10.0
default -> node (-> v12.10.0)
node -> stable (-> v12.10.0) (default)
stable -> 12.10 (-> v12.10.0) (default)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/dubnium (-> v10.16.3)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.16.1 (-> N/A)
lts/dubnium -> v10.16.3

In the output above, the entry with an arrow on the right (-> v8.9.4), is the version used in the current shell session and the default version is set to v12.3.1.

The default version is the version that will be used when you open new shell (aka. terminal) session windows.

To change the currently active version to v10.16.3 use the following command:

nvm use 10.16.3

and verify it by typing:

nvm current

If you want to set version 10.16.3 as the new default version when you open a new shell session window, use alias default:

nvm alias default 10.16.3

Conclusion

Hopefully this was a simple walkthrough to show you how to setup NVM and Node.js on the Raspberry Pi. NVM is a popular tool in most desktop environments, so it’s great to also have that option on the Raspberry Pi!