What is Node.js
Node.js is an open-source environment that enables you to use Javascript on the server-side. It's also a cross-platform language available for Windows, Linux, FreeBSD, macOS, and other platforms. Before Node.js came into the web development world, Javascript was mainly used to create a frontend for websites, web applications, and handling users interactions. Previously, the most popular languages used to manage server requests were PHP and ASP (and are still used today). The main difference between PHP or ASP and Node.js is how they handle file requests.
How PHP or ASP Handles a File Request
Let's look at how PHP or ASP would handle a file request.
- The request is sent to the file system of the computer.
- Waits for the file system to open and read the file.
- Return the results to the client
- Ready to take on the subsequent request.
How Node.js Handles a File Request
Here's how Node.js handles a file request
- The request is sent to the file system of the computer.
- Ready to take on the subsequent request.
- Return the results to the client after reading and opening the file.
You can see Node.js eliminates the waiting time before handling the subsequent request.
What is NPM
NPM, on the other hand, stands for Node Package Manager. NPM is two things: An online platform and a Command-Line tool. The online platform allows users to share and publish various tools written in Javascript. If you have worked with NPM before, these are the tools that you download as modules in your projects. The tools can be used in Browser (Front End), Server (Back End), or Command-Line (CLI).
NPM as a command-line tool enables users to interact with the NMP online platform. Here are some of the things you can do with the NPM command-line tool.
- Install and uninstall packages
- Version management: Ensures each package on your system is up to date with the latest release of the package on the online platform.
- Dependency management
This post will give you a detailed guide on installing Node.js and NPM on Kali Linux. There are two ways which you can use to install Node.js and NPM on Kali Linux.
- By adding the Node.js repository
- By compiling Node.js source code
Method-1: Install Node.js From Repository
Repositories on Linux are online storage locations where users can download and update packages. Adding a particular repository to your Kali Linux system lets you download the specified package using the APT command. Although, we don't necessarily need to add the Node.js repo to our system since it's already present on the Kali Linux official repositories.
However, there is a catch! The Node.js package present on Kali Linux repositories is version 12, yet newer releases like version 14 are available for download. To solve that, we will add our repositories to download the latest Node.js version. Follow the steps below.
Step 1: Add Node.js Repository
Execute the command below on your terminal.
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
Sample Output:
Step 2: Update System
After adding the node repository, run the update command below to flush the system's repository cache. That lets the system know you have added a new repository.
sudo apt update
Step 3: Install Node.js Version 14 on Kali Linux
Up to this point, we have everything present on our system to download and install the latest version of Node.js and NPM. Execute the command below.
sudo apt install nodejs
Sample Output:
Step 4: Check Node and NPM Versions
Tip:Â Node.js installer includes the NPM package manager. Therefore, by installing Node.js, you also install NPM.
To check the installed version of Node.js, run the command below.
node -v
To check the installed version of NPM, run the command below.
npm -v
Sample Output:
Step 5: Remove Node.js and NPM
To uninstall Node.js and NPM from your system, run the command below:
sudo apt remove nodejs
Sample Output:
Method-2: Install Node.js By Compiling the Source Code
This method can be a little complicated, but it's best if the first method fails. To get started, navigate to the official Node.JS website and download the Node.js Source Code. It can also be used to install Node.js and NPM on any other Linux distribution. Once the download completes successfully, launch the terminal and navigate to the file's location using the cd
command. Follow the steps below.
Step 1: Extract the Compressed File
The source code is a .tar.xz
compressed file. To extract it, use the syntax below.
tar -xvf [file-name] e.g tar -xvf node-v14.18.0-linux-x64.tar.xz
After extracting the file, you will see a new node directory. Navigate to this directory using the cd
command as shown below.
cd node-v14.18.0-linux-x64
Step 2: Configure the Source Code For Installation
Run the command below to configure and build the source for installation.
sudo ./configure
Sample Output:
When done, run the command below.
sudo make -j2
The parameter -j2
specifies the number of CPUs you want to use. In our case, we have set it to two. This process might take quite some time. Please be patient. When done, run the command below to install Node.js and NPM.
sudo make install
Step 3: Check Version
To check the installed version of Node.js, run the command below.
node -v
To check the installed version of NPM, run the command below.
npm -v
Sample Output:
Using NPM on Kali Linux
To install a package with NPM, we will use the syntax below:
npm install <package-name> e.g npm install bootstrap-icons
You might find other commands using the i
parameter instead of install. However, they all work the same way.
npm i bootstrap-icons
If you wish to update the NPM package manager, run the command below.
npm install -g npm@next
Conclusion
This post has given you a detailed guide on installing NPM and Node.js on your Kali Linux system. If you have come across any issues or errors, please share in the comments below and we will help where we can.