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.
1. Which Version to Install?
Node.js releases come in two tracks:
| Version Type | Description | When 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 |
2. Installation Methods
Method 1: Official Installer (Windows & macOS)
Windows
- Download the Windows installer from nodejs.org (choose the LTS version).
- Run the
.msiinstaller. - Follow the installation wizard (default settings are recommended).
- Restart your terminal.
- Verify the installation:
node --version # Should show v22.x.x
npm --version # Should show the npm version
macOS
- Download the macOS installer from nodejs.org.
- Run the
.pkginstaller. - Follow the installation wizard.
- 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.
| Command | Description | Example |
|---|---|---|
node | Start Node.js REPL (interactive shell) | node |
node file.js | Execute a JavaScript file | node app.js |
node --version | Show Node.js version | node -v |
node --eval | Execute code directly | node -e "console.log('hi')" |
node --inspect | Start with debugging enabled | node --inspect app.js |
node --watch | Watch files for changes (Node.js 18+) | node --watch app.js |
node --check | Syntax check without execution | node -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.
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 Type | Command | Where it's installed | When to use |
|---|---|---|---|
| Local | npm install express | node_modules/ in your project | Project dependencies (most packages) |
| Global | npm install -g nodemon | System-wide location | CLI tools, utilities |
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
sudofor 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
npxor local installs. - ✅ Set memory limits — Use
--max-old-space-sizefor 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
Ready to master Node.js?
Build scalable, high-performance backend applications with Node.js. Learn from industry experts with hands-on projects.
.png)