Raspberry Pi

Install Node.js on Raspberry Pi 4

Node.js is an open-source and cross-platform server-side JavaScript runtime powered by the Google Chrome V8 JavaScript engine. Node.js is mainly used to develop network apps, APIs, and full-stack web apps. Node.js can also be used to develop desktop apps and mobile apps.

In this article, I am going to show you how to install the latest LTS (Long Term Support) version of Node.js on Raspberry Pi 4 running the Raspberry Pi OS. So, let’s get started.

Table of Contents:

  1. Downloading Node.js for Raspberry Pi 4
  2. Installing Node.js on Raspberry Pi 4
  3. Writing a Hello World Program in Node.js
  4. Conclusion

Downloading Node.js for Raspberry Pi 4

You can download the latest LTS version of Node.js for your Raspberry Pi 4 from the official website of Node.js.

To do that, visit the URL https://nodejs.org/en/download/ from your favorite web browser. Once the page loads, click on the Linux ARMv7 ARM binary download link as marked in the screenshot below.

NOTE: At the time of this writing, the latest LTS version of Node.js is v16.13.1.

The Node.js ARM v7 binary archive should be downloaded.

Installing Node.js on Raspberry Pi 4

The Node.js ARM v7 binary archive should be downloaded in the ~/Downloads directory.

Navigate to the ~/Downloads directory as follows:

$ cd ~/Downloads

The latest LTS version of Node.js ARM v7 binary archive node-v16.13.1-linux-armv7l.tar.xz (in my case) should be available in the ~/Downloads directory as you can see in the screenshot below.

$ ls -lh

Extract the archive node-v16.13.1-linux-armv7l.tar.xz in the /opt directory with the following command:

$ sudo tar xvf node-v16.13.1-linux-armv7l.tar.xz -C /opt

The Node.js ARM v7 binary archive node-v16.13.1-linux-armv7l.tar.xz should be extracted in the /opt directory.

Navigate to the /opt directory as follows:

$ cd /opt

You should see a new directory (node-v16.13.1-linux-armv7l/ in this case) as marked in the screenshot below.

$ ls -lh

Rename the directory node-v16.13.1-linux-armv7l/ to node/ so that the commands in the next sections will be shorter and easier to write.

$ sudo mv -v node-v16.13.1-linux-armv7l node

Notice that the node and npm binaries are in the /opt/node/bin/ directory.

$ ls /opt/node/bin/

To access and run the node and npm commands, you will have to make symbolic links of these binary files in the /usr/bin/ directory.

Run the following command to make a symbolic link of the /opt/node/bin/node binary in the path /usr/bin/node:

$ sudo ln -s /opt/node/bin/node /usr/bin/node

Run the following command to make a symbolic link of the /opt/node/bin/npm binary in the path /usr/bin/npm:

$ sudo ln -s /opt/node/bin/npm /usr/bin/npm

Restart your Raspberry Pi 4 with the following command:

$ sudo reboot

Once your Raspberry Pi 4 starts, you should be able to run the node and npm commands as shown in the screenshot below.

$ node --version
$ npm --version

Writing a Hello World Program in Node.js:

In this section, I am going to show you how to write a Hello World Node.js program and run it on Raspberry Pi 4.

First, create a ~/project directory as follows:

$ mkdir -v ~/project

Then, open your favorite text editor or IDE, create a new file app.js, type in the following lines of codes, and save the file in the ~/project/ directory.

Then, navigate to the ~/project directory as follows:

$ cd ~/project

Run app.js with Node.js as follows:

$ node app.js

The app.js script should run and print the correct output as you can see in the screenshot below.

Conclusion:

In this article, I have shown you how to install the latest LTS version of Node.js on Raspberry Pi 4 running the Raspberry Pi OS. I have also shown you how to write a simple Node.js program and run it with Node.js on Raspberry Pi 4.

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.