• Articles
  • Tutorials
  • Interview Questions

Downloading and Installing Node.js and NPM

Downloading and Installing Node.js and NPM

Before diving into the process of downloading and installing Node.js and NPM, there are a few essential prerequisites to consider. Ensure your system meets the minimum requirements, such as an operating system compatible with Node.js, a stable internet connection, and ample storage space. Familiarize yourself with basic command line usage, as it will be necessary during the installation process. Having a clear understanding of JavaScript fundamentals will also enhance your learning experience.

Table of Contents:

Unlock the power of React JS on Windows with our step-by-step tutorial! Learn how to install and set up React JS effortlessly – watch now!

Video Thumbnail

Getting Started with Node.js and NPM

Getting Started with Node.js and NPM

Node.js and NPM (Node Package Manager) are powerful tools widely used in modern web development. Node.js is an open-source JavaScript runtime environment built on Chrome’s V8 JavaScript engine. 

NPM, on the other hand, is the default package manager for Node.js. Together, they provide developers with a robust and efficient environment for server-side and command-line JavaScript applications. This article will explore the fundamental concepts and features of Node.js and NPM.

Node.js: Node.js enables developers to execute JavaScript code outside of the web browser, thereby facilitating the creation of network applications that are scalable and high-performing. Its core functionality lies in providing an event-driven, non-blocking I/O model, which ensures the efficient handling of concurrent requests without obstructing the flow of execution. 

This distinctive asynchronous approach positions Node.js as an optimal choice for developing real-time applications, exemplified by its suitability for constructing chat servers and streaming platforms.

Key Features of Node.js:

  • JavaScript Runtime: Node.js enables developers to use JavaScript for both client-side and server-side development, providing a consistent language and toolset across different layers of an application.
  • Single-Threaded Event Loop: Node.js utilizes a single-threaded event loop to handle multiple concurrent requests efficiently. This event-driven architecture enables scalable, non-blocking I/O operations, improving performance and responsiveness.
  • Extensive Package Ecosystem: Node.js benefits from a vast collection of open-source packages available through NPM. These packages provide reusable modules and libraries that streamline development, enhance functionality, and foster collaboration within the developer community.
  • Cross-Platform Compatibility: Node.js exhibits cross-platform compatibility, rendering it highly versatile for deployment across diverse operating systems such as Windows, macOS, and Linux.

NPM (Node Package Manager): NPM, which stands for Node Package Manager, serves as a comprehensive package manager designed specifically for Node.js. It offers developers a user-friendly interface to effortlessly install, administer, and distribute reusable JavaScript code modules. 

By granting access to an expansive repository of packages, NPM empowers developers to capitalize on preexisting solutions, significantly expediting the development process.

Key Features of NPM:

  • Package Installation and Version Management: NPM helps you to simplify the installation and management of the packages by providing a command-line interface. Developers can specify package dependencies in a package.json file, allowing consistent and reproducible deployments.
  • Version Control and Dependency Resolution: NPM maintains a version control system that helps the developers to track and manage package versions effectively. It also resolves dependencies automatically, ensuring that the correct versions of packages are installed.
  • Publishing and Collaboration: NPM facilitates publishing and sharing packages, enabling developers to contribute to the open-source community. It offers tools for managing package ownership, versioning, and security, fostering collaboration and knowledge exchange.
  • Script Execution: NPM enables developers to specify custom scripts within the package.json file, automating repetitive procedures like testing, building, and deployment. This functionality significantly enhances productivity and fosters the adoption of consistent development practices.

Download and Install Node.js and NPM on Windows

Download and Install Node.js and NPM on Windows

There are multiple ways by which you can download Node.js and NPM on your Windows system. 

Official Node.js Website:

  • Visit the official Node.js website at https://nodejs.org.
  • Go to the “Downloads” page.
  • Choose the recommended version (usually the LTS version).
  • Click on the “Windows Installer” button to download the installer.
  • Once the download is complete, run the installer and follow the on-screen instructions to install Node.js and NPM.

Package Managers:

  • Chocolatey: If you have Chocolatey installed, you can run the CMD or Windows PowerShell and run the command choco install nodejs to automatically download and install Node.js and NPM.
  • Scoop: If you use the Scoop package manager, open the Command Prompt or PowerShell and run the command scoop install nodejs to install Node.js and NPM.

Node Version Manager (NVM):

  • Node Version Manager allows you to engage multiple versions of Node.js on your computer system.
  • Start by installing NVM by visiting https://github.com/coreybutler/nvm-windows and downloading the latest installer.
  • Execute the downloaded installer and adhere to the provided instructions to install NVM.
  • Open a new CMD or Windows PowerShell window and use the NVM command nvm install stable to install the latest updated stable version of Node.js along with NPM.

Steps to install Node.js and NPM in Windows:

Step 1: Access the Official Node.js Website: 

Open the browser of your choice and navigate to the official Node.js website (https://nodejs.org). Ensure that you are on the Downloads page, which automatically detects your operating system.

Step 2: Choose the Appropriate Windows Installer:

On the Downloads page, you will find two options: LTS (Long-Term Support) and Current. For most users, it is recommended to select the LTS version as it offers stability and compatibility. Click on the Windows Installer button corresponding to your system architecture (32-bit or 64-bit).

Step 3: Run the Installer:

Once the installer is downloaded, locate the executable file and double-click on it to initiate the installation process. If a security warning appears, confirm that you want to proceed.

Step 4: Accept the License Agreement:

The Node.js installer will present you with the End-User License Agreement (EULA). Read the agreement carefully, and if you agree, click the checkbox indicating your acceptance. Then, click on the “Next” button.

Step 5: Choose Installation Options:

In this step, you can select additional features to be installed alongside Node.js. However, feel free to customize the installation according to your needs. Click the “Next” button to continue.

Sep 6: Select the Installation Location:

Choose the folder where you want to install Node.js. The default location is usually suitable for most users. Click “Next” to proceed.

Step 7: Complete the Installation:

Once you have reviewed the installation settings, select the Install tab to start the process of installation. A progress bar will pop up, and it will display the installation status. After completion, click “Finish” to exit the installer.

Step 8: Verify the Installation

To ensure that Node.js and NPM (Node Package Manager) are successfully installed, open the Command Prompt or PowerShell and type “node -v” and “npm -v” respectively. If the installed versions are displayed, congratulations! You have successfully installed Node.js and NPM on your Windows system.

How to Uninstall Node.js and NPM on Windows? 

By following this step-by-step guide, you can successfully uninstall Node.js and NPM from your Windows operating system. Remember to verify the uninstallation and clean up the environment variables to ensure complete removal. Uninstalling Node.js and NPM will free up system resources and allow you to start with a clean slate or install a different version if necessary.

Step 1: Locate the Node.js Installation Directory

  1. Open the File Explorer on your Windows machine.
  2. Navigate to the directory where Node.js is installed. The default location is typically “C:\Program Files\nodejs” or “C:\Program Files (x86)\nodejs“.

Step 2: Uninstall Node.js

  1. In the Node.js installation directory, locate and run the “uninstall” executable. It is usually named “uninstall” or “unins000” and has the Node.js logo.
  2. Follow the on-screen prompts to uninstall Node.js from your system. If you encounter any confirmation dialog boxes, select “Yes” or “Uninstall” to proceed.
  3. Wait for the uninstallation process to complete. It may take a few moments.

Step 3: Verify Node.js Uninstallation

  1. Open the Command Prompt or PowerShell.
  2. Type “node -v” and press Enter. If Node.js is uninstalled successfully, you will be displayed an error message indicating that the command is unrecognized.
  3. Additionally, you can type “npm -v” and press Enter. If NPM is uninstalled correctly, you should receive an error message like the one above.

Step 4: Remove the Node.js Installation Directory

  1. Open the File Explorer.
  2. Navigate to the Node.js installation directory (“C:\Program Files\nodejs” or “C:\Program Files (x86)\nodejs”).
  3. Right-click on the directory and select Delete.
  4. Confirm the deletion when prompted.

Step 5: Clean Up Environment Variables

  1. Open the Control Panel on your Windows system.
  2. Search for Environment Variables and select Edit the system environment variables.
  3. Navigate to the System Properties window, and click the Environment Variables button.
  4. Under the System Variables option,  you have to locate the “Path” variable.
  5. Choose the Path variable and select the “Edit” button.
  6. Remove references to the Node.js installation directory from the “Path” variable.
  7. Select the OK button that will save the changes and close all open windows.

Download and Install Node.js and NPM on MacOSX

Download and Install Node.js and NPM on MacOSX

Installing Node.js and NPM on macOS is a fundamental step for developers seeking to leverage the power of JavaScript beyond the browser. This sub-section of the blog offers a concise and formal walkthrough to download and install Node.js and NPM on macOS, ensuring a smooth setup process. By following these instructions, you’ll have the necessary tools to embark on your JavaScript development journey confidently.

Steps to install Node.js and NPM in macOSX:

Step 1: Downloading Node.js and NPM:

  • Start your default web browser and visit the official Node.js website. (https://nodejs.org).
  • On the homepage of the installation wizard, you will find the “Download” button. Click on it and proceed further to the page where the download is available.
  • The website will automatically detect your OS if it doesn’t, make sure to select the appropriate macOS version from the provided options.
  • Choose the LTS (Long-Term Support) version of Node.js, as it provides a stable and reliable platform.
  • Once you’ve selected the version, click on the downloaded package to begin the installation process.

Step 2: Installing Node.js and NPM:

  • Run the downloaded package to launch the Node.js installer.
  • Follow the on-screen instructions provided to proceed with the further installation process.
  • When prompted, enter your administrator password to authorize the installation.
  • After the installation completes, open the Terminal application, which can be found in the Utilities folder within the Applications folder.
  • Verify that Node.js and NPM are installed correctly by typing the following commands in the Terminal:
  • node -v (to check the Node.js version)
  • npm -v (to check the NPM version) If both commands display the version numbers, you have successfully installed Node.js and NPM on your macOS.

How to Uninstall Node.js and NPM on MacOSX?

Uninstalling Node.js and NPM (Node Package Manager) from your macOS system can be necessary when upgrading or troubleshooting. Following these instructions carefully will ensure a clean uninstallation and avoid any potential conflicts or issues.

Step 1: Remove Global NPM Packages: 

Before uninstalling Node.js and NPM, removing any globally installed packages is essential. Open the Terminal application on your Mac and enter the following command:

npm ls -g --depth=0

This command will list all the globally installed NPM packages on your system. Take note of the packages you want to keep; you will need to reinstall them later.

To uninstall a global package, use the following command:

npm uninstall -g <package-name>

Change <package-name> with the package name you want to remove. Repeat this command for each package you wish to uninstall.

Step 2: Remove Node.js and NPM: 

To uninstall Node.js and NPM from your macOS system, you can choose between manual removal or a package manager like Homebrew.

Method 1: Manual Removal

  • Open the Terminal application.
  • Enter the following command to delete Node.js and NPM files:
sudo rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/{npm*,node*,man1/node*}
  • Provide your password when prompted.

Method 2: Homebrew

If you have installed Node.js and NPM using Homebrew, you can use it to uninstall them:

  • Open the Terminal application.
  • Enter the following command to uninstall Node.js:
brew uninstall node

Step 3: Verify the Uninstallation: 

To confirm that Node.js and NPM have been successfully uninstalled from your Mac, open the Terminal and type:

node -v

If Node.js is uninstalled, the Terminal will display an error message stating that the command “node” was not found.

Additionally, you can check if NPM is uninstalled by entering the following:

npm -v

If NPM is removed, the Terminal will display an error message stating that the command “npm” was not found.

Download and Install Node.js and NPM on Linux

Download and Install Node.js and NPM on Linux

Here is the step-by-step tutorial on downloading and installing Node.js and NPM on Linux.

Step 1: Updating System Packages

Update system packages before installing Node.js, it is recommended to update the system packages. 

sudo apt update
sudo apt upgrade

Step 2: Install Node.js 

To install Node.js, we will use the NodeSource repository, which provides the latest Node.js versions. Execute the following commands in the terminal:

curl -sL https://deb.nodesource.com/setup_lts.x | sudo -E 
sudo apt install -y nodejs

This will add the NodeSource repository and install the LTS (Long-Term Support) version of Node.js.

Step 3: Verify the Installation: 

Run the following commands to install and check the Node.js and NPM versions:

node -v
npm -v

If the installation process is successful, the terminal will display the installed versions of Node.js and NPM.

Step 4: Install Development Tools (Optional) 

If you plan to build native addons, it is recommended to install development tools. Run the following command:

sudo apt install -y build-essential

Step 5: Updating NPM (Optional) 

To update NPM to the latest version, execute the following command:

sudo npm install -g npm

How to Uninstall Node.js and NPM on Linux?

By following these instructions, you can uninstall Node.js and NPM from your Linux-based system efficiently and without leaving any remnants behind.

Step 1: Verify the existing installation. 

Before proceeding with the uninstallation process, verifying if Node.js and NPM are already installed on your system is important. Open a terminal and type the following commands:

node -v
npm -v

If both commands return the version numbers, Node.js and NPM are installed.

Step 2: Uninstall Node.js. 

To uninstall Node.js, you will need to remove the Node.js binary and associated files. Execute the following commands in the terminal:

sudo apt remove nodejs
sudo apt autoremove

These commands will remove Node.js and its dependencies from your system. The second command will also remove unused packages to free up disk space.

Step 3: Uninstall NPM 

NPM is the package manager for Node.js. To uninstall NPM, run the following command in the terminal:

sudo apt remove npm

This command will remove NPM and any associated configuration files.

Step 4: Confirm the uninstallation. 

To confirm that Node.js and NPM have been successfully uninstalled, you can once again run the commands from Step 1. If both commands return an error or are not recognized, it indicates a successful uninstallation.

Hello World Node.js Application

Once you have Node.js downloaded and installed on your computer, let’s try to display “Hello World” in a web browser. To run this application, save the code in a file with a .js extension (e.g., hello.js), and execute it using the Node.js runtime environment (node hello.js). The node server will start running, and you can access the “Hello, World!” message by visiting http://localhost:3000 in your web browser or sending a request programmatically.

// Create a simple HTTP server
const http = require('http');
const hostname = 'localhost';
const port = 3000;
const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello, World!\n');
});
// Start the server
server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Explanation:

  • We begin by importing the built-in http module, which allows us to create an HTTP server in Node.js.
  • Next, we define the hostname (in this case, ‘localhost’) and the port number (3000) on which our server will listen for incoming requests.
  • Using the http.createServer() method, we create an instance of the HTTP server. This method takes a callback() as an argument, which will be invoked whenever a request is made to our server.
  • Inside the callback function, we set the response status code to 200, indicating a successful request. We also set the ‘Content-Type’ header to ‘text/plain’, specifying that we are sending plain text as the response.
  • Finally, we send the response with the message ‘Hello, World!\n’ and end the response using the res.end() method.
  • Lastly, we call the server.listen() method to start the server and make it listen on the specified hostname and port. The callback() will be invoked once the server is running, and it logs a message indicating the server’s address.

Conclusion

Congratulations on successfully completing the journey of downloading and installing Node.js and NPM! By now, you’ve realized the immense power and endless possibilities that these tools offer to JavaScript developers. With Node.js, you can build scalable, server-side applications and leverage the vast ecosystem of NPM packages. 

Remember to keep exploring and learning as Node.js and NPM continuously evolve. Stay connected with the thriving developer community, where you’ll find support, guidance, and inspiration in this particular domain.

Course Schedule

Name Date Details
Web Development Courses 14 Dec 2024(Sat-Sun) Weekend Batch View Details
21 Dec 2024(Sat-Sun) Weekend Batch
28 Dec 2024(Sat-Sun) Weekend Batch

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.