Node.js Installation & Setup
Complete guide for Windows, macOS, and Linux
Beginner Friendly
All Platforms
SEO Ready
Table of Contents
What is NPM?
NPM (Node Package Manager) is the default package manager for Node.js and the world's largest software registry with over 2 million packages.
| Aspect | Details |
|---|---|
| Full Form | Node Package Manager |
| Created by | Isaac Schlueter (2010) |
| Packages available | Over 2 million |
| Purpose | Share, discover, and reuse code |
| License | Open Source (Artistic License 2.0) |
What Can You Do with NPM?
npm install express
npm install -g nodemon
npm install --save-dev jest
npm run start
npm update
npm uninstall express
Why Do You Need NPM?
- Code Reusability - avoid rewriting common tools
- Version Management - lock exact/compatible package versions
- Dependency Management - auto handles transitive dependencies
- Script Running - start/test/build commands from one place
- Sharing Code - publish reusable packages
Pre-installation Checklist
| Platform | Minimum Version | RAM | Disk Space |
|---|---|---|---|
| Windows | Windows 10 / Server 2016 | 4GB | 1GB |
| macOS | macOS 10.13+ | 4GB | 1GB |
| Linux | Ubuntu 18.04 / Debian 9 / RHEL 7 | 4GB | 1GB |
Check if Node.js is Already Installed
node --version
npm --version
# If command not found, continue installation
Windows Installation
Method 1: Official Installer (Recommended)
- Visit nodejs.org and download LTS `.msi`.
- Run installer, accept license, keep default path `C:\Program Files\nodejs\`.
- Ensure options enabled: Node runtime, npm, Add to PATH.
- Install and restart if prompted.
Method 2: Chocolatey
# Run as Administrator
Set-ExecutionPolicy Bypass -Scope Process -Force
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
choco install nodejs
choco install nodejs-lts
Method 3: Winget
winget search nodejs
winget install OpenJS.NodeJS.LTS
winget install OpenJS.NodeJS
macOS Installation
Method 1: Official Installer
- Download `.pkg` from nodejs.org.
- Open installer, continue, accept license, and install.
- Enter Mac password when prompted.
Method 2: Homebrew
# Install Homebrew (if missing)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install node
node --version
npm --version
brew upgrade node
Method 3: NVM (Best for multiple versions)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
source ~/.zshrc
nvm install --lts
nvm install 20.10.0
nvm use 20.10.0
nvm alias default 20.10.0
nvm list
How to Run Node.js on macOS
After installation, use Terminal to create and run your first Node.js program on macOS.
1) Verify Installation
node -v
npm -v
2) Create and Run a Simple File
mkdir mac-node-demo
cd mac-node-demo
printf "console.log('Hello from macOS Node.js');\n" > app.js
node app.js
3) Run an HTTP Server
printf "const http=require('http');const server=http.createServer((req,res)=>{res.end('Node on macOS works!');});server.listen(3000,()=>console.log('http://localhost:3000'));\n" > index.js
node index.js
Open http://localhost:3000 in your browser. For development workflow, initialize npm and use scripts: npm init -y, then npm start / npm run dev.
Linux Installation
Ubuntu/Debian
sudo apt update
sudo apt install nodejs npm
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
sudo apt install -y build-essential
Fedora/RHEL/CentOS
sudo dnf module enable nodejs:20
sudo dnf install nodejs npm
curl -sL https://rpm.nodesource.com/setup_20.x | sudo bash -
sudo yum install nodejs
Arch Linux
sudo pacman -S nodejs npm
yay -S nodejs
NVM (Any Linux)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
source ~/.bashrc
nvm install --lts
nvm install 20.10.0
nvm use 20.10.0
Manual Binaries
wget https://nodejs.org/dist/v20.10.0/node-v20.10.0-linux-x64.tar.xz
tar -xf node-v20.10.0-linux-x64.tar.xz
sudo mv node-v20.10.0-linux-x64 /usr/local/node
export PATH=/usr/local/node/bin:$PATH
source ~/.bashrc
Verifying Installation
node --version
node -v
npm --version
npm -v
node
# > console.log("Hello, Node.js!");
# > .exit
npm help
# Windows
where node
where npm
# macOS/Linux
which node
which npm
NPM Deep Dive
NPM Architecture
+-----------------------------------------+
| NPM Registry (registry.npmjs.org) |
+-------------------+---------------------+
|
v
+-----------------------------------------+
| package.json / node_modules / lockfile |
+-----------------------------------------+
Create package.json
npm init
npm init -y
Common NPM Commands
| Command | Description | Example |
|---|---|---|
| npm init | Create package.json | npm init -y |
| npm install | Install dependencies | npm install express |
| npm install -g | Global install | npm install -g nodemon |
| npm uninstall | Remove package | npm uninstall express |
| npm outdated | Check outdated packages | npm outdated |
| npm run | Run scripts | npm run start |
Global vs Local Installation
| Aspect | Local Installation | Global Installation |
|---|---|---|
| Command | npm install package | npm install -g package |
| Location | ./node_modules/ | System directory |
| Use for | Project dependencies | CLI tools |
| Examples | express, react | nodemon, yarn |
Semantic Versioning
Version: 4.18.2
| | +-- Patch
| +---- Minor
+------ Major
^4.18.2 # Compatible major
~4.18.2 # Compatible minor
Common Installation Issues & Solutions
Windows: "Node is not recognized"
Add `C:\Program Files\nodejs\` to PATH and restart terminal.
Windows: Global permission errors
npm config get prefix
mkdir ~\AppData\Roaming\npm-global
npm config set prefix ~\AppData\Roaming\npm-global
macOS: command not found / EACCES
export PATH="/usr/local/bin:$PATH"
echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.zshrc
sudo chown -R $(whoami) ~/.npm
Linux: old version from package manager
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
# or use nvm install 20.10.0
First Project Setup
Create Project
mkdir my-first-node-app
cd my-first-node-app
git init
npm init -y
Create index.js
// index.js
console.log("Welcome to Node.js!");
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('<h1>Hello from Node.js!</h1>');
res.write('<p>Your first server is running!</p>');
res.end();
});
const PORT = 3000;
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});
Install Packages and Run
npm install express
npm install --save-dev nodemon
npm start
npm run dev
Project Structure Template
my-node-app/
|- node_modules/
|- src/
| |- controllers/
| |- models/
| |- routes/
| |- utils/
|- public/
|- tests/
|- .env
|- .gitignore
|- index.js
|- package.json
Sample .gitignore
node_modules/
npm-debug.log
.env
dist/
build/
.DS_Store
Thumbs.db
.vscode/
.idea/
coverage/
Yarn Alternative
npm install -g yarn
yarn init
yarn add express
yarn add --dev nodemon
yarn remove express
10 Interview Questions + 10 MCQs
Interview Pattern 10 Q&A1Why is LTS preferred over Current in production?easy
Answer: LTS offers longer support, stability, and fewer breaking changes for production workloads.
2How do you manage multiple Node versions on one machine?easy
Answer: Use NVM to install and switch versions per project/environment.
3What is the role of package-lock.json?medium
Answer: It locks exact dependency tree for reproducible installs across environments and CI.
4How do local and global npm installs differ?easy
Answer: Local installs are project-specific dependencies; global installs provide system-wide CLI tools.
5How do you secure npm usage in projects?medium
Answer: Run
npm audit, keep dependencies updated, pin versions if needed, and avoid untrusted packages.6What causes EACCES errors and how do you fix them?medium
Answer: Permission ownership issues on global directories; fix by changing npm prefix/user ownership or using nvm.
7Why add Node path to PATH variable?easy
Answer: So shells can find
node and npm commands from any directory.8When would you use Homebrew/Chocolatey instead of installer?medium
Answer: For repeatable CLI-based setup, scripted provisioning, and easier upgrades.
9What is a good first check when Node commands fail?easy
Answer: Verify versions and executable location using
node -v, npm -v, and which/where node.10Why keep scripts in package.json?medium
Answer: It standardizes project commands for all team members and CI systems.
10 Node.js Installation MCQs
1
Best Node.js version for production most of the time?
Explanation: LTS is preferred for long-term stability.
2
Which command quickly creates package.json defaults?
Explanation:
npm init -y skips prompts and writes defaults.3
Global package install flag is:
Explanation: Use
-g for global installation.4
Which tool helps manage multiple Node versions?
Explanation: NVM installs/switches Node versions easily.
5
Which file should be committed for lock consistency?
Explanation: Commit lockfile; do not commit node_modules.
6
Command to check vulnerabilities in dependencies?
Explanation:
npm audit checks package vulnerabilities.7What does
What does ^4.18.2 usually allow?
Explanation: Caret allows compatible updates under same major.
8
Which command shows where Node executable is located?
Explanation: Use
which (macOS/Linux) or where (Windows).9
Which package is typically installed as dev dependency?
Explanation:
nodemon is usually a development-only tool.10
Best first action if terminal still cannot find node after install?
Explanation: PATH changes often apply after reopening terminal or restart.