What is NodeJS
Nodejs or Node is an open-source and cross-platform runtime environment for executing Javascript code outside of a browser. We often use Node to build back-end services, also called APIs (Application Programming Interfaces). These services power our client application, like a web app running inside a web browser or a mobile app running on a mobile device. These client apps are simply what a user sees and interacts with. They are just the surface and need to talk to some services sitting on the server or in the cloud to store data, send emails or push notifications, kick-off workflows, and so on. Node is ideal for building highly scalable data-intensive and real-time back-end services that power our client applications.
What is NPM
On the other hand, NPM stands for Node Package Manager. You can think of NPM as two things:
- An online platform
- A command-line tool
NPM, as an online platform, is a place where developers can publish and share tools written in Javascript. Anyone can search and use tools available on this online platform. You can find tools for use in the browser, server, or even the command line.
NPM, as a command-line tool, helps developers interact with the NPM online platform. You can use NPM to install and uninstall packages available on the NPM online platform. NPM is also used to manage package versions and dependencies.
Install NodeJS and NPM on Raspberry Pi
There are two main ways you can use to install NodeJS and NPM on your Raspberry Pi.
- Install NodeJS and NPM from the NodeSource repository
- Install NodeJS and NPM from source code.
This post will look at all the methods above, and you can pick one that suits you.
Lab Requirements
- A Raspberry Pi running the Official Raspberry Pi OS: For better performance, we highly recommend using Raspberry Pi board 3 or 4.
- An active internet connection
- A reliable power supply
Method 1: Install NodeJS and NPM From the NodeSource Repo
NodeSource is a platform focused on helping developers and organizations run production-ready NodeJS apps by providing enterprise-grade Node support. It maintains an APT repo where you can download the latest version of NodeJS on your system. Follow the steps below.
Step 1: Enable the NodeSource Repository
Launch the Terminal and execute the command below. In our case, we are connecting to the Raspberry Pi over SSH.
sudo su
curl -fsSL https://deb.nodesource.com/setup_17.x | bash -
Step 2: Install NodeJS
When done, run the command below to install NodeJS on your system.
sudo apt install nodejs
Step 3: Verify Installation
When done, verify NodeJS and NPM installation by running the version command below.
node --version
npm --version
Uninstall/ Remove NodeJS and NPM
If you wish to uninstall NodeJS and NPM installed using this method, run the command below.
sudo apt remove nodejs
Method 2: Install NodeJS and NPM From Source Code
As the title suggests, this method can get a little complicated since you need to compile and build the NodeJS source code yourself. This method is also limited to specific Raspberry Pi boards, as we will see below. Follow the steps below.
Step 1: Update and Upgrade Your System
Since you will compile the source code o your system, you need to ensure that you are running the latest packages to avoid errors. Execute the command below on your Raspberry Pi.
sudo apt update
sudo apt upgrade
The upgrade
command can take quite some time to finish depending on your internet connection and when you last upgraded your Pi.
Step 2: Check the Node Version You Need to Download
As said earlier, this method can only be used on specific Pi boards architecture depending on the node source code available. To know the architecture on which your PI is running on, execute the command below.
uname -m
From the image above, you can see we are running on armv7l
. This architecture version will guide us in downloading the required ode source code as described in the next step.
Step 3: Download NodeJS
Open your browser and navigate to the NodeJS Download Page. You will see the available binaries for different platforms and architecture, including Windows, Linux, macOS, x64, and ARM. We need to download the ARM binaries. Now you need to download the specific binary for your architecture. In our case, we are running armv7l. Therefore, we will download NodeJS binaries for ARMV7L.
Instead of clicking the download link and downloading the package using our browser or download manager, we will use the wget command. Right-click on the download links and select the option "copy link address" or "copy file location." Launch the Terminal on your Pi and use the syntax below to download the package.
sudo wget https://nodejs.org/dist/v16.13.0/node-v16.13.0-linux-armv7l.tar.xz
When done, run the ls command, and you should see a node package.
Step 4: Extract the File
The package downloads in a compressed format; to extract it, use the syntax below.
sudo tar -xvf [file-name]
e.g
sudo tar -xvf node-v16.13.0-linux-armv7l.tar.xz
When you rerun the ls
command, you will now see a node directory.
Step 5: Copy the Extracted Files to Directory Path
To make Node available system-wide for the currently logged-in user, we need to copy the extracted file to the /usr/local directory. To get started, first navigate to the newly created directory using the cd command as shown below.
cd node-v16.13.0-linux-armv7l
We will use the command below to copy all the contents to the /usr/local directory.
sudo cp -R * /usr/local
Let's discuss the command above in detail:
- -R: This parameter means that "copy all the files recursively." That enables us to copy a directory and all its contents.
- *: This asterisk means that we are copying everything in our current directory.
That's it! We have successfully installed NodeJS and NPM on our system.
Step 6: Verify Installation
When done, verify NodeJS and NPM installation by running the version command below.
node --version
npm --version
Conclusion
This post has given you a step-by-step guide on installing NodeJS and NPM on your system. If you encounter any errors while executing any of the commands above, please tell us in the comments, and we will help where we can.
So I continued with: