Install Node.js 16 and npm on Raspberry Pi

Node.js is an open-source runtime environment that allows to run JavaScript code on the server without a browser. It is based on Chrome’s V8 JavaScript engine. Node.js is commonly used to develop web applications. NPM is a package manager for Node.js that allows to install packages from remote registry.

Node.js 16 is a long-term support (LTS) release. In this release the V8 JavaScript engine has been updated from 8.6 to 9.0. This tutorial shows how to install Node.js 16 and npm on Raspberry Pi.

Install Node.js and npm

Connect to Raspberry Pi via SSH and add NodeSource repository that maintains Node.js 16 binary distributions.

curl -sSL https://deb.nodesource.com/setup_16.x | sudo bash -

Run command to install Node.js:

sudo apt install -y nodejs

Note that, we don’t need to install npm separately, it comes bundled with Node.js. After the installation is complete, check Node.js and npm versions:

node --version
npm --version

Testing Node.js

Navigate to your home directory and create a new main.js file:

cd ~
nano main.js

When a file is opened, add the following code:

main.js

console.log('Hello world');

Now use the node command to test a program:

node main.js

Uninstall Node.js and npm

We can remove any package with a name that starts with nodejs and anything related to it by running the following command:

sudo apt purge -y nodejs.*

We can also remove NodeSource repository and signing key:

sudo rm -rf /etc/apt/sources.list.d/nodesource.list
sudo rm -rf /usr/share/keyrings/nodesource.gpg

Install older versions

Node.js 14 is older LTS version that is still supported. It can be installed by adding the following NodeSource repository:

curl -sSL https://deb.nodesource.com/setup_14.x | sudo bash -

This article has 2 comments

Leave a Comment

Your email address will not be published. Required fields are marked *