Node.js is a powerful JavaScript runtime that allows developers to build scalable network applications. NPM (Node Package Manager) is bundled with Node.js and helps manage dependencies efficiently. This guide will walk you through installing and configuring Node.js and NPM on a Windows machine.
Step 1: Download Node.js Installer
- Visit the official Node.js website.
- Choose the LTS (Long-Term Support) version for stability and security.
- Download the Windows Installer (.msi) file for your system architecture (64-bit or 32-bit).
Step 2: Install Node.js
- Run the downloaded .msi installer.
- Follow the on-screen instructions:
- Accept the license agreement.
- Choose the installation location.
- Ensure the option “Automatically install the necessary tools” is checked (for compiling native modules).
- Click Next and complete the installation process.
Step 3: Verify Installation
Once installed, verify that Node.js and NPM are properly set up:
- Open Command Prompt (cmd) or PowerShell.
- Type the following commands:
node -v
npm -v
If installed correctly, these commands will display the installed versions of Node.js and NPM.
Step 4: Configure NPM (Optional)
Set Default Global Directory for NPM Packages
To avoid permission errors when installing global packages, you can change the default directory:
- Create a directory for global packages:
mkdir "%USERPROFILE%\npm-global"
- Configure NPM to use this directory:
npm config set prefix "%USERPROFILE%\npm-global"
- Add the new directory to the system’s PATH:
- Open Control Panel > System > Advanced system settings.
- Click Environment Variables.
- Edit the Path variable under User variables.
- Add: %USERPROFILE%\npm-global\bin
Step 5: Install Yarn (Optional)
Yarn is an alternative package manager that can be installed using NPM:
npm install -g yarn
Verify the installation:
yarn -v
Step 6: Test Node.js by Running a Simple Script
To ensure everything is working, create a test file:
- Open a text editor and save the following as
app.js
:console.log("Hello, Node.js!");
- Run the script using:
node app.js
You should see Hello, Node.js!
printed in the terminal.
Conclusion
You have successfully installed and configured Node.js and NPM on Windows. You are now ready to start building JavaScript applications and managing dependencies efficiently!