How to Install npm and Node.js - Complete Guide for All Platforms

Mahesh Mahesh Waghmare
7 min read

Node.js and npm (Node Package Manager) are essential tools for modern JavaScript development. Whether you’re building web applications, working with React, Vue, or Angular, or using build tools like Webpack or Vite, you’ll need Node.js and npm installed.

This comprehensive guide covers installation methods for Windows, macOS, and Linux, plus version management and troubleshooting tips.

Understanding Node.js and npm

Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. It allows you to run JavaScript on the server-side and build command-line tools.

npm (Node Package Manager) comes bundled with Node.js and is the default package manager for the JavaScript ecosystem. It allows you to:

  • Install and manage JavaScript packages
  • Share your own packages
  • Manage project dependencies
  • Run scripts and build tools

Key Points:

  • Installing Node.js automatically installs npm
  • npm version is tied to Node.js version
  • Both should be kept updated
  • Version managers allow multiple versions

Windows Installation

The easiest way to install Node.js on Windows:

  • 1
    Visit nodejs.org
  • 2
    Download the LTS (Long Term Support) version for Windows
  • 3
    Run the installer (.msi file)
  • 4
    Follow the installation wizard
  • 5
    Check "Automatically install necessary tools" if prompted
  • 6
    Restart your computer if required
  • 7
    Verify installation: Open Command Prompt and run node --version

Installation Options:

  • LTS Version: Recommended for most users (stable, long-term support)
  • Current Version: Latest features, may be less stable

What Gets Installed:

  • Node.js runtime
  • npm package manager
  • Adds Node.js to system PATH
  • npm global folder configuration

Method 2: Using Chocolatey

If you have Chocolatey package manager installed:

choco install nodejs

Update:

choco upgrade nodejs

Method 3: Using Winget

Windows Package Manager (built into Windows 10/11):

winget install OpenJS.NodeJS.LTS

Method 4: Using Scoop

If you use Scoop package manager:

scoop install nodejs
Advertisement

macOS Installation

Method 1: Official Installer

The simplest method for macOS:

  1. Visit nodejs.org
  2. Download the macOS installer (.pkg file)
  3. Open the downloaded file
  4. Follow the installation wizard
  5. Verify: Open Terminal and run node --version

Homebrew is the most popular package manager for macOS:

Install Homebrew (if not installed):

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Install Node.js:

brew install node

Update Node.js:

brew upgrade node

Benefits of Homebrew:

  • Easy updates
  • Manages dependencies automatically
  • Can install multiple versions

Method 3: Using MacPorts

If you use MacPorts:

sudo port install nodejs18

Method 4: Using nvm (Node Version Manager)

For managing multiple Node.js versions:

Install nvm:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

Install Node.js:

nvm install --lts
nvm use --lts

Linux Installation

Ubuntu/Debian

Method 1: Using NodeSource Repository (Recommended)

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

Method 2: Using Default Repository

sudo apt update
sudo apt install nodejs npm

Note: Default repository may have older versions.

Fedora/RHEL/CentOS

Using NodeSource Repository:

curl -fsSL https://rpm.nodesource.com/setup_lts.x | sudo bash -
sudo yum install -y nodejs

Or using dnf:

sudo dnf install nodejs npm

Arch Linux

sudo pacman -S nodejs npm

openSUSE

sudo zypper install nodejs npm

Using Version Managers

Version managers allow you to install and switch between multiple Node.js versions, which is useful for:

  • Testing compatibility
  • Working on multiple projects
  • Following project requirements

nvm (Node Version Manager) - macOS/Linux

Installation:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

Usage:

nvm install 18.17.0        # Install specific version
nvm install --lts          # Install latest LTS
nvm use 18.17.0           # Switch to version
nvm list                  # List installed versions
nvm alias default 18.17.0 # Set default version

nvm-windows (Windows)

Installation:

  1. Download from github.com/coreybutler/nvm-windows/releases
  2. Run the installer
  3. Restart Command Prompt

Usage:

nvm install 18.17.0
nvm use 18.17.0
nvm list

fnm (Fast Node Manager) - Cross-platform

Installation (macOS/Linux):

curl -fsSL https://fnm.vercel.app/install | bash

Installation (Windows):

winget install Schniz.fnm

Usage:

fnm install 18.17.0
fnm use 18.17.0
fnm list

Verify Installation

After installation, verify everything works:

Check Node.js Version

node --version
# or
node -v

Should display version number (e.g., v18.17.0).

Check npm Version

npm --version
# or
npm -v

Should display npm version (e.g., 9.6.7).

Test Installation

Create a test file test.js:

console.log('Node.js is working!');

Run it:

node test.js

Should output: Node.js is working!

Check Installation Paths

Node.js location:

which node        # macOS/Linux
where node        # Windows

npm location:

which npm         # macOS/Linux
where npm         # Windows

Check Global npm Configuration

npm config list
npm config get prefix

Updating Node.js and npm

Updating Node.js

Windows/macOS (Official Installer):

  1. Download latest installer from nodejs.org
  2. Run installer (it will update existing installation)
  3. Verify with node --version

Homebrew (macOS):

brew upgrade node

Linux (NodeSource):

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

Using Version Managers:

nvm install --lts
nvm use --lts

Updating npm

npm can be updated independently:

npm install -g npm@latest

Check for updates:

npm outdated -g

Update to specific version:

npm install -g npm@9.6.7

Troubleshooting Common Issues

Issue: “node is not recognized”

Windows: Node.js not in PATH

  • Reinstall Node.js and check “Add to PATH” option
  • Manually add to PATH: C:\Program Files\nodejs\
  • Restart Command Prompt completely

macOS/Linux: Installation incomplete

  • Verify installation: which node
  • Check PATH: echo $PATH
  • Reinstall if necessary

Issue: npm Command Not Found

Solution:

  1. Verify Node.js is installed: node --version
  2. npm should come with Node.js
  3. Reinstall Node.js if npm is missing
  4. Check npm location: which npm or where npm

Issue: Permission Errors (macOS/Linux)

Global installs failing:

sudo npm install -g package-name

Better solution - Change npm prefix:

mkdir ~/.npm-global
npm config set prefix '~/.npm-global'

Add to ~/.bashrc or ~/.zshrc:

export PATH=~/.npm-global/bin:$PATH

Issue: Version Conflicts

Multiple Node.js versions:

  • Use version manager (nvm, fnm)
  • Uninstall conflicting versions
  • Use version manager to switch versions

Issue: Outdated npm

Update npm:

npm install -g npm@latest

Clear npm cache:

npm cache clean --force
Advertisement

Best Practices

Version Selection

  1. Use LTS for Production: LTS versions are stable and supported longer
  2. Use Current for Development: Try new features, but test thoroughly
  3. Match Team Versions: Use same version as your team
  4. Check Project Requirements: Some projects specify Node.js versions

Installation Location

  1. Default Locations: Usually fine for most users
  2. Custom Locations: Only if you have specific requirements
  3. Version Managers: Recommended for developers working on multiple projects

Maintenance

  1. Regular Updates: Keep Node.js and npm updated
  2. Security Patches: Install security updates promptly
  3. Clean Uninstalls: Remove old versions to avoid conflicts
  4. Backup Configurations: Save npm configuration if customized

Project Management

  1. Use package.json: Always use package.json for project dependencies
  2. Lock Files: Commit package-lock.json to version control
  3. Node Version: Specify Node.js version in .nvmrc or package.json
  4. CI/CD: Use same Node.js version in CI/CD as development

Security

  1. Audit Dependencies: Regularly run npm audit
  2. Update Dependencies: Keep dependencies updated
  3. Use Trusted Sources: Only install from npm registry or trusted sources
  4. Review Packages: Check package reputation before installing

Conclusion

Installing Node.js and npm is straightforward with the right method for your platform:

  • Windows: Official installer is easiest
  • macOS: Homebrew or official installer
  • Linux: Use NodeSource repository for latest versions
  • Multiple Versions: Use version managers (nvm, fnm)

After installation:

  1. Verify with node --version and npm --version
  2. Test with a simple script
  3. Keep both updated regularly
  4. Use version managers for flexibility

Node.js and npm are essential tools for modern JavaScript development. Proper installation and maintenance ensure a smooth development experience.

Advertisement
Mahesh Waghmare

Written by Mahesh Waghmare

I bridge the gap between WordPress architecture and modern React frontends. Currently building tools for the AI era.

Follow on Twitter

Read Next