Back to Course
Installation & CLI

Node.js Installation Guide & CLI Basics

Before you can build anything with Node.js, you need to get it installed and understand the essential command-line tools. This guide will walk you through installing Node.js on Windows, macOS, and Linux, and introduce you to the Node.js CLI, npm, npx, and version management — the tools you'll use every single day as a Node.js developer.

Production Reality: Your development environment setup is the foundation of your productivity. A correctly configured Node.js installation with the right version management strategy will save you hours of debugging later.

1. Which Version to Install?

Node.js releases come in two tracks:

Version TypeDescriptionWhen to Use
LTS (Long-Term Support) Stable, well-tested, receives security updates for 30 months ✅ Production — Always use LTS in production
Current (Latest) Latest features, experimental, updates every 2 weeks ⚠️ Development — For testing new features only
Best Practice: Always use the latest LTS version for production applications. Check nodejs.org for the current LTS version. As of writing, Node.js 22 LTS is the recommended version.

2. Installation Methods

Method 1: Official Installer (Windows & macOS)

Windows

  1. Download the Windows installer from nodejs.org (choose the LTS version).
  2. Run the .msi installer.
  3. Follow the installation wizard (default settings are recommended).
  4. Restart your terminal.
  5. Verify the installation:
node --version   # Should show v22.x.x
npm --version     # Should show the npm version

macOS

  1. Download the macOS installer from nodejs.org.
  2. Run the .pkg installer.
  3. Follow the installation wizard.
  4. Verify the installation:
node --version
npm --version

Method 2: Package Managers (Linux & macOS)

Ubuntu/Debian

# Using NodeSource repository (recommended)
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt-get install -y nodejs

# Using apt (may have older versions)
sudo apt update
sudo apt install nodejs npm

macOS (Homebrew)

# Install Homebrew first (if not installed)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# Install Node.js
brew install node

# Verify
node --version
npm --version

Fedora/RHEL/CentOS

curl -fsSL https://rpm.nodesource.com/setup_22.x | sudo bash -
sudo dnf install nodejs

Arch Linux

sudo pacman -S nodejs npm

Windows (Chocolatey)

choco install nodejs-lts

Method 3: Version Managers (Recommended for Production)

This is the most recommended approach for professional developers. Version managers allow you to:

  • Install and switch between multiple Node.js versions instantly.
  • Test your application against different Node.js versions.
  • Use different versions for different projects.
  • Easily upgrade or downgrade versions.

nvm (Node Version Manager) — Most Popular

Installation:

# macOS / Linux
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash

# Windows (use nvm-windows)
# Download from: https://github.com/coreybutler/nvm-windows/releases

Essential nvm Commands:

# Install the latest LTS version
nvm install --lts

# Install a specific version
nvm install 20.10.0

# List installed versions
nvm ls

# Switch to a specific version
nvm use 20.10.0

# Set a default version
nvm alias default 20.10.0

# List all available remote versions
nvm ls-remote

n (Simple Version Manager for macOS/Linux)

# Install n
curl -L https://bit.ly/n-install | bash

# Install the latest LTS
n lts

# Install a specific version
n 20.10.0

# Switch version
n

fnm (Fast Node Manager — Written in Rust)

# Install using Homebrew (macOS)
brew install fnm

# Install using Winget (Windows)
winget install Schniz.fnm

# Install LTS
fnm install --lts

# Use a version
fnm use 20.10.0

3. Verifying Your Installation

After installation, run these commands to verify everything is working correctly:

# Check Node.js version
node -v

# Check npm version
npm -v

# Check npx version
npx -v

# Print Node.js installation path
which node

# Print npm installation path
which npm

# Check if Node.js is working
node -e "console.log('Hello, Node.js!')"

4. Node.js CLI — Essential Commands

The Node.js CLI is your primary interface for running Node.js applications and scripts.

CommandDescriptionExample
nodeStart Node.js REPL (interactive shell)node
node file.jsExecute a JavaScript filenode app.js
node --versionShow Node.js versionnode -v
node --evalExecute code directlynode -e "console.log('hi')"
node --inspectStart with debugging enablednode --inspect app.js
node --watchWatch files for changes (Node.js 18+)node --watch app.js
node --checkSyntax check without executionnode -c file.js

5. npm — Node Package Manager

npm is the largest package registry in the world, with over 2 million packages. It's automatically installed with Node.js.

Essential npm Commands

# Initialize a new project
npm init                      # Interactive
npm init -y                   # Skip questions (defaults)

# Install packages
npm install express           # Install a specific package
npm install                   # Install all dependencies from package.json
npm install --save-dev jest   # Install as dev dependency
npm install -g nodemon        # Install globally

# Uninstall packages
npm uninstall express         # Remove a package

# Update packages
npm update                    # Update all packages
npm update express            # Update a specific package

# Run scripts
npm start                     # Run the "start" script
npm test                      # Run the "test" script
npm run build                 # Run a custom script

# View information
npm list                      # List installed packages
npm list --depth=0            # List only top-level packages
npm outdated                  # Check for outdated packages

# Publish packages
npm publish                   # Publish your package to npm

# Security
npm audit                     # Check for vulnerabilities
npm audit fix                 # Fix vulnerabilities

package.json — The Heart of Every Node.js Project

{
    "name": "my-project",
    "version": "1.0.0",
    "description": "My Node.js application",
    "main": "index.js",
    "scripts": {
        "start": "node index.js",
        "dev": "nodemon index.js",
        "test": "jest",
        "build": "webpack --mode production"
    },
    "dependencies": {
        "express": "^4.18.0",
        "mongoose": "^7.0.0"
    },
    "devDependencies": {
        "jest": "^29.0.0",
        "nodemon": "^2.0.0"
    }
}

package-lock.json

The package-lock.json file locks the exact versions of every dependency. This ensures that every developer and production environment uses the exact same dependency versions, preventing "it works on my machine" problems.

Production Best Practice: Always commit package-lock.json to version control. Never use npm install without --lockfile in production. Use npm ci instead of npm install in CI/CD pipelines for faster, deterministic builds.

6. npx — Execute Packages Without Installing

npx is a tool that comes with npm (since version 5.2.0). It allows you to execute Node.js packages without installing them globally.

# Execute a package without installing
npx create-react-app my-app   # Creates a React app

npx express-generator         # Generates an Express app

npx mocha                    # Run tests with mocha

npx prettier --write .       # Format code with Prettier

npx npm-check-updates -u     # Update all dependencies to latest

7. Global vs Local Installations

Install TypeCommandWhere it's installedWhen to use
Localnpm install expressnode_modules/ in your projectProject dependencies (most packages)
Globalnpm install -g nodemonSystem-wide locationCLI tools, utilities
Production Warning: Avoid global installations in CI/CD and production environments. Always use local installations and npx for executable tools. This ensures consistency across environments.

8. Common Installation Issues & Solutions

Issue 1: "command not found: node"

Solution: Node.js is not in your PATH. Restart your terminal. On Windows, you may need to restart your computer. Alternatively, add the Node.js installation path manually to your system's PATH environment variable.

Issue 2: Permission Errors (EACCES, EPERM)

Solution: This is a common issue on Linux/macOS. Fix it by:

  • Use sudo for global installations (not recommended).
  • Better: Reinstall Node.js using a version manager (nvm) which doesn't require sudo.
  • Or change the npm global directory: mkdir ~/.npm-global && npm config set prefix ~/.npm-global

Issue 3: Version Mismatch Across Environments

Solution: Use a version manager (nvm). Add a .nvmrc file to your project root:

# .nvmrc
20.10.0

Then users can run nvm use to automatically switch to the correct version.

Issue 4: npm Install Fails with SSL Errors

Solution: Try these steps:

  • Update npm: npm install -g npm@latest
  • Clear cache: npm cache clean --force
  • Use HTTP instead of HTTPS: npm config set registry http://registry.npmjs.org/
  • Check your network/proxy settings.

Issue 5: Node.js Uses Too Much Memory

Solution: Increase the memory limit:

# For Node.js (increase to 4GB)
node --max-old-space-size=4096 app.js

Or set an environment variable: export NODE_OPTIONS="--max-old-space-size=4096"

9. Production-Ready Checklist

  • ✅ Install LTS version — Always use the latest LTS for production.
  • ✅ Use a version manager — nvm (or fnm) for easy version switching.
  • ✅ Add .nvmrc — Lock the Node.js version for your team.
  • ✅ Commit package-lock.json — Ensure deterministic builds.
  • ✅ Use npm ci — In CI/CD pipelines instead of npm install.
  • ✅ Avoid global packages in production — Use npx or local installs.
  • ✅ Set memory limits — Use --max-old-space-size for production.
  • ✅ Verify installation — Test Node.js and npm are working correctly.
  • ✅ Keep npm updated — Regularly update npm for security patches.
  • ✅ Use npm audit — Regularly check for vulnerabilities.

10. Quick Reference — One-Liner Commands

# Install Node.js (macOS)
brew install node

# Install Node.js (Ubuntu/Debian)
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash - && sudo apt-get install -y nodejs

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

# Install latest LTS with nvm
nvm install --lts && nvm use --lts

# Create a new Node.js project
mkdir my-app && cd my-app && npm init -y

# Install Express
npm install express

# Run a Node.js app
node app.js

# Run with auto-restart (nodemon)
npm install -g nodemon && nodemon app.js
Production Wisdom: A properly configured development environment is the foundation of productive development. Take the time to set up your tools correctly — it will pay dividends in saved time, fewer bugs, and smoother deployments throughout your career.

Ready to master Node.js?

Build scalable, high-performance backend applications with Node.js. Learn from industry experts with hands-on projects.

Explore Course