Installing and Configuring Node.js

1. Installing Node.js

MethodCommand/ToolNotes
Official installernodejs.orgLTS recommended (v20+ LTS)
macOS Homebrewbrew install nodeLatest stable
Linux aptapt install nodejs npmAdd NodeSource repo first
Windows wingetwinget install OpenJS.NodeJS.LTSBuilt-in package manager
Dockerdocker pull node:20-alpineSmallest image (~50MB)
Volta / fnm / nvmvolta install nodePer-project version pinning

2. Managing Node Versions

ToolPlatformStrength
nvmmacOS / LinuxMost popular, shell-based
nvm-windowsWindowsSeparate project, similar API
fnmCross-platformRust-based, very fast
VoltaCross-platformPins versions per package.json
asdfmacOS / LinuxPolyglot version manager

3. Checking Node Version

CommandOutput
node -v / node --versionv20.11.1
process.versionInside script
process.versionsnode, v8, openssl, etc.

4. Checking npm Version

CommandPurpose
npm -vCurrent npm version
npm install -g npm@latestUpdate npm globally
npm doctorDiagnose installation issues

5. Using Node Version Manager

nvm CommandAction
nvm install 20Install Node 20.x
nvm use 20Switch active version
nvm alias default 20Default for new shells
nvm ls / nvm ls-remoteList installed / available
nvm use --ltsSwitch to latest LTS

Example: .nvmrc auto-switch

echo "20.11.1" > .nvmrc
nvm use            # reads .nvmrc

6. Configuring npm Registry

CommandPurpose
npm config get registryShow current registry
npm config set registry https://registry.npmjs.org/Set default registry
npm config set @scope:registry https://npm.pkg.github.comScoped registry (private)
.npmrcPer-project / per-user config file

7. Setting Up Global Install Directory

SettingCommand
Show prefixnpm config get prefix
Set prefixnpm config set prefix ~/.npm-global
Add to PATHexport PATH=~/.npm-global/bin:$PATH
Note: Avoids needing sudo for global installs and prevents permission issues.

8. Configuring Environment Variables

VariablePurpose
NODE_ENVdevelopment / production / test
NODE_OPTIONSDefault CLI flags (e.g. --max-old-space-size=4096)
NODE_PATHExtra module resolution paths
NODE_TLS_REJECT_UNAUTHORIZEDSet 0 to disable cert checks (dev only)
NODE_DEBUGEnable internal debug logs (e.g. http,fs)
UV_THREADPOOL_SIZElibuv 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)
      
ComponentRole
V8Executes JavaScript, JIT compilation, GC
libuvEvent loop, async I/O, thread pool
BindingsBridge between JS and C++ APIs
Node CoreBuilt-in modules (fs, http, etc.)

10. Configuring Node.js for Production

SettingRecommendation
NODE_ENV=productionDisables debug, enables optimizations in many libs
--max-old-space-size=4096Set heap limit (MB)
Process managerPM2, systemd, or Kubernetes for restarts
Reverse proxyNginx / Caddy in front of Node
LoggingJSON structured logs to stdout
Cluster / Worker ThreadsUse all CPU cores