Introduction
Node.js is a runtime environment that includes everything you need to execute a program written in JavaScript. It's used for running scripts on servers to render content before it is delivered to a web browser.
Node Package Manager (NPM) is an application manager and repository for developing and sharing JavaScript code. It enables managing Node.js dependencies.
This guide shows several methods to install and test Node.js and NPM on Windows.
Prerequisites
- A user account with administrator access (or the ability to download and install software).
- Access to the command prompt.
- A text editor or IDE to run code and test Node.js.
Note: To install Node.js on different operating systems, check out our guides:
Install Node.js and NPM on Windows via Node.js Installer
If you prefer using the GUI to install programs, Node.js has a downloadable installer on their official website. Follow the steps below to install Node.js and NPM on Windows via the installer.
Step 1: Download Node.js Installer
In a web browser, navigate to the Node.js Downloads page. Click the Windows Installer button to download the latest stable version with long-term support (LTS). The installer also includes the NPM package manager.
The file is saved in the Downloads folder by default.
Other versions of Node.js and NPM are available, so choose the appropriate one for your system and use case. Use the top tabs to switch from the LTS to the current version to test the newest features. If you are new to Node.js or don't need a specific version, choose LTS since it is tested and stable.
Step 2: Install Node.js and NPM
After downloading the installer, follow the steps below:
1. Launch the installer by double-clicking the downloaded file.
2. The Node.js Setup Wizard starts with the welcome screen.
Click Next to proceed.
4. Review the end-user license agreement and click the checkbox to accept the terms and conditions.
Click Next to continue.
5. The installer asks to choose the installation location.
Leave the default location for a standard installation and click Next to proceed.
6. Select components to include or remove from the installation. The default options install Node.js, NPM, corepack, online documentation shortcuts, and add the programs to PATH.
Customize the setup or click Next to accept the default values.
7. The following section shows the total required space for the installation and the available space on disk.
Click OK to proceed. Select a different disk or install fewer features if the installation does not allow proceeding.
8. Choose whether to install additional dependencies for compiling native modules. Some NPM modules compile from C/C++ and require additional tools to function correctly (Python, Visual Studio Build Tools, and Chocolatey).
If you use such modules, select the checkbox and click Next. The selection of this option starts an installation script after the Node.js installation is complete.
For a simple installation, skip this step and click Next to proceed.
7. Click the Install button to start the installation.
8. The installer prompts for administrator confirmation to make changes to the device.
Enter the administrator password if prompted and click Yes to continue.
9. The installation takes some time. When it is complete, the final screen shows a success message.
Click Finish to complete the installation and close the installer.
Note: If you selected to install native tools in step 8, closing the installer automatically starts a PowerShell script to install the listed tools.
Step 3: Verify Installation
To verify Node.js installed successfully, run the following command in a command prompt or PowerShell:
node -v
The command shows the Node.js version installed on your system. Use the following command to check for NPM:
npm -v
Note: If NPM is not recognized or properly installed, command prompt displays the following error message: npm: command not found. Check if NPM is added to path, or if there are multiple versions installed on the system.
Install Node.js and NPM on Windows via Chocolatey
Chocolatey is a Windows package manager. It can be used to install programs such as Node.js with NPM. Follow the steps below to install Node.js and NPM using Chocolatey.
Note: Chocolatey is not available on Windows by default. If you need to install it, follow our guide to install Chocolatey on Windows.
Step 1: Install Node.js and NPM
To run the installation command, do the following:
1. Open the command prompt as an administrator.
2. Run the following command to install Node.js and NPM:
choco install nodejs
When prompted to run the installation script, press y and Enter to continue the installation. Wait for the installation to complete before proceeding.
3. Reset environment variables to add Node.js and NPM to PATH. Use the following command:
refreshenv
The command resets the environment variables, which enables running both programs from the terminal.
Step 2: Verify Node.js and NPM installation
To verify that Node.js is installed correctly, run the following command in a new command prompt session:
node -v
Check the NPM installation with:
npm -v
Both commands display the program version.
Test Node.js Installation
There are several ways to test a Node.js installation. The simplest way is to create a Hello World JavaScript program and run it using Node.js:
1. Open a text editor or IDE.
2. Add the following code to the file:
console.log("Hello, world!")
The code is a simple program that prints "Hello, world!" to the console.
3. Save the script with a .js extension.
4. Run the program from an IDE to see the results. Alternatively, navigate to the directory where the file is located and use the following command:
node [file_name].js
The command prints the message to the console, indicating that the Node.js installation works successfully.
Conclusion
You should now be able to install the Node.js framework and the NPM package manager. You also wrote your first Node.js JavaScript program.
Next, see the differences between Yarn and NPM, two different Node.js package managers.