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:
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 -
The 5 Comments Found
thank you so much for this great tutorial, it was very helpful.
Thank you!
Very interesting blog post.Quite informative and very helpful. I’m a regular reader of your blog Thank you for providing such a nice piece of article. I’m glad to leave a comment. Expect more articles in the future.
I’m struggling to get Node v16 installed on ARMv6. Anyone able to get past this with the first command?
## Installing the NodeSource Node.js 16.x repo...
## You appear to be running on ARMv6 hardware. Unfortunately this is not currently supported by the NodeSource Linux distributions. Please use the 'linux-armv6l' binary tarballs available directly from nodejs.org for Node.js 4 and later.
Hi,
NodeSource repository doesn't provide Node.js for ARMv6 architecture. You can try to find unofficial builds and install manually.
Leave a Comment
Cancel reply