Installing and Configuring Node.js
1. Installing Node.js
| Method | Command/Tool | Notes |
| Official installer | nodejs.org | LTS recommended (v20+ LTS) |
| macOS Homebrew | brew install node | Latest stable |
| Linux apt | apt install nodejs npm | Add NodeSource repo first |
| Windows winget | winget install OpenJS.NodeJS.LTS | Built-in package manager |
| Docker | docker pull node:20-alpine | Smallest image (~50MB) |
| Volta / fnm / nvm | volta install node | Per-project version pinning |
2. Managing Node Versions
| Tool | Platform | Strength |
| nvm | macOS / Linux | Most popular, shell-based |
| nvm-windows | Windows | Separate project, similar API |
| fnm | Cross-platform | Rust-based, very fast |
| Volta | Cross-platform | Pins versions per package.json |
| asdf | macOS / Linux | Polyglot version manager |
3. Checking Node Version
| Command | Output |
node -v / node --version | v20.11.1 |
process.version | Inside script |
process.versions | node, v8, openssl, etc. |
4. Checking npm Version
| Command | Purpose |
npm -v | Current npm version |
npm install -g npm@latest | Update npm globally |
npm doctor | Diagnose installation issues |
5. Using Node Version Manager
| nvm Command | Action |
nvm install 20 | Install Node 20.x |
nvm use 20 | Switch active version |
nvm alias default 20 | Default for new shells |
nvm ls / nvm ls-remote | List installed / available |
nvm use --lts | Switch to latest LTS |
Example: .nvmrc auto-switch
echo "20.11.1" > .nvmrc
nvm use # reads .nvmrc
6. Configuring npm Registry
| Command | Purpose |
npm config get registry | Show current registry |
npm config set registry https://registry.npmjs.org/ | Set default registry |
npm config set @scope:registry https://npm.pkg.github.com | Scoped registry (private) |
.npmrc | Per-project / per-user config file |
7. Setting Up Global Install Directory
| Setting | Command |
| Show prefix | npm config get prefix |
| Set prefix | npm config set prefix ~/.npm-global |
| Add to PATH | export PATH=~/.npm-global/bin:$PATH |
Note: Avoids needing sudo for global installs and prevents permission issues.
8. Configuring Environment Variables
| Variable | Purpose |
NODE_ENV | development / production / test |
NODE_OPTIONS | Default CLI flags (e.g. --max-old-space-size=4096) |
NODE_PATH | Extra module resolution paths |
NODE_TLS_REJECT_UNAUTHORIZED | Set 0 to disable cert checks (dev only) |
NODE_DEBUG | Enable internal debug logs (e.g. http,fs) |
UV_THREADPOOL_SIZE | libuv worker pool size (default 4) |
9. Understanding Node.js Runtime Architecture
Architecture Stack
JavaScript Code (your app)
↓
Node.js APIs (fs, http, crypto, ...)
↓
Bindings (C++ glue)
↓ ↓
V8 libuv (event loop, thread pool)
↓ ↓
OS (syscalls, network, filesystem)
| Component | Role |
| V8 | Executes JavaScript, JIT compilation, GC |
| libuv | Event loop, async I/O, thread pool |
| Bindings | Bridge between JS and C++ APIs |
| Node Core | Built-in modules (fs, http, etc.) |
10. Configuring Node.js for Production
| Setting | Recommendation |
NODE_ENV=production | Disables debug, enables optimizations in many libs |
--max-old-space-size=4096 | Set heap limit (MB) |
| Process manager | PM2, systemd, or Kubernetes for restarts |
| Reverse proxy | Nginx / Caddy in front of Node |
| Logging | JSON structured logs to stdout |
| Cluster / Worker Threads | Use all CPU cores |