Node.js Installation & Setup

Complete guide for Windows, macOS, and Linux

Beginner Friendly All Platforms SEO Ready

Table of Contents

  1. What is NPM?
  2. Pre-installation Checklist
  3. Windows Installation
  4. macOS Installation
  5. Linux Installation
  6. Verifying Installation
  7. NPM Deep Dive
  8. Common Installation Issues & Solutions
  9. First Project Setup

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.

AspectDetails
Full FormNode Package Manager
Created byIsaac Schlueter (2010)
Packages availableOver 2 million
PurposeShare, discover, and reuse code
LicenseOpen 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

PlatformMinimum VersionRAMDisk Space
WindowsWindows 10 / Server 20164GB1GB
macOSmacOS 10.13+4GB1GB
LinuxUbuntu 18.04 / Debian 9 / RHEL 74GB1GB

Check if Node.js is Already Installed

node --version
npm --version
# If command not found, continue installation

Windows Installation

Method 1: Official Installer (Recommended)

  1. Visit nodejs.org and download LTS `.msi`.
  2. Run installer, accept license, keep default path `C:\Program Files\nodejs\`.
  3. Ensure options enabled: Node runtime, npm, Add to PATH.
  4. 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

  1. Download `.pkg` from nodejs.org.
  2. Open installer, continue, accept license, and install.
  3. 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

CommandDescriptionExample
npm initCreate package.jsonnpm init -y
npm installInstall dependenciesnpm install express
npm install -gGlobal installnpm install -g nodemon
npm uninstallRemove packagenpm uninstall express
npm outdatedCheck outdated packagesnpm outdated
npm runRun scriptsnpm run start

Global vs Local Installation

AspectLocal InstallationGlobal Installation
Commandnpm install packagenpm install -g package
Location./node_modules/System directory
Use forProject dependenciesCLI tools
Examplesexpress, reactnodemon, 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&A
1Why 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?

ACurrent
BLTS
CBeta
DNightly
Explanation: LTS is preferred for long-term stability.
2

Which command quickly creates package.json defaults?

Anpm init -y
Bnode init
Cnpm create file
Dnpx init
Explanation: npm init -y skips prompts and writes defaults.
3

Global package install flag is:

A--global-install
B-g
C--save-dev
D-l
Explanation: Use -g for global installation.
4

Which tool helps manage multiple Node versions?

ANVM
BWebpack
CPM2
DESLint
Explanation: NVM installs/switches Node versions easily.
5

Which file should be committed for lock consistency?

Apackage-lock.json
Bnode_modules/
C.env
Dnpm-debug.log
Explanation: Commit lockfile; do not commit node_modules.
6

Command to check vulnerabilities in dependencies?

Anpm doctor
Bnpm audit
Cnpm check
Dnpm secure
Explanation: npm audit checks package vulnerabilities.
7

What does ^4.18.2 usually allow?

AOnly 4.18.2 exact
BAny 4.x.x compatible
COnly patch updates
DAny major version
Explanation: Caret allows compatible updates under same major.
8

Which command shows where Node executable is located?

Awhich node / where node
Bnode path
Cnpm locate
Dnode --install-path
Explanation: Use which (macOS/Linux) or where (Windows).
9

Which package is typically installed as dev dependency?

Aexpress
Bnodemon
Cpg
Daxios
Explanation: nodemon is usually a development-only tool.
10

Best first action if terminal still cannot find node after install?

ARestart terminal/PC
BDelete package-lock.json
CRun npm publish
DDisable firewall
Explanation: PATH changes often apply after reopening terminal or restart.