Install & Setup
Getting Node running takes a few minutes. Do it right once and you avoid version pain forever.
Check What You Have
Open a terminal and run:
node --version
npm --version
npm ships with Node, so you get both. If the commands are missing, install Node.
Use a Version Manager
Different projects need different Node versions. A version manager lets you switch instantly:
# nvm (macOS / Linux)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
# fnm (fast, cross-platform)
brew install fnm
# Windows: use nvm-windows or fnm
Then install a current LTS release:
nvm install --lts
nvm use --lts
LTS (Long Term Support) is the stable choice for real projects.
Your First Script
Create a file called hello.js:
const name = process.argv[2] ?? "world";
console.log(`Hello, ${name}!`);
Run it:
node hello.js
node hello.js Ada
process.argv holds the command-line arguments. Index 2 is the first one you passed.
Initialize a Project
Every project should have a package.json. Generate one:
npm init -y
This records your dependencies and scripts. Run commands with:
npm run dev
Keep Node Out of Git
Add node_modules to .gitignore — dependencies are reinstallable from package.json.
node_modules/
.env
Best Practices
- Pin the version — Add an
.nvmrcwith20and commit it. - Use LTS — Avoid odd-numbered and brand-new releases in production.
- Commit
package-lock.json— It locks exact dependency versions. - Never commit
node_modules— Or.envfiles with secrets.
Common Mistakes
- Installing with
sudo— It breaks permissions; use a version manager. - Mixing managers — Pick
nvmorfnm, not both. - Ignoring engine warnings —
enginesinpackage.jsontells teammates the required version. - Forgetting to restart the shell — Newly installed managers need a reload.