>_cat nodejs.md|Web & App Servers
Node.js Cheatsheet
SPEC: MDX·/nodejs·HTTP reverse proxies, application runtimes, and log analysis.
Node.js is a cross-platform, open-source JavaScript runtime environment built on Chrome's V8 engine.
Installation
Debian / Ubuntu
# Via NodeSource (e.g. Node.js 20 LTS)
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejsRHEL / CentOS / Fedora
# Via NodeSource
curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash -
sudo dnf install -y nodejsArch Linux
sudo pacman -S nodejs npmmacOS
brew install nodeNVM (Node Version Manager)
Install NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
# OR
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrcManage Versions
nvm install 20 # Install Node.js v20
nvm install --lts # Install latest LTS release
nvm use 20 # Switch to Node.js v20 in current shell
nvm use default # Switch to configured default version
nvm alias default 20 # Set default Node.js version
nvm uninstall 18 # Remove Node.js v18Inspection
nvm ls # List locally installed versions
nvm ls-remote # List available remote versions
nvm current # Print active versionNode.js CLI Execution
Running Scripts
node app.js # Execute JS file
node -e "console.log(1+1)" # Evaluate inline JS string
node -p "process.versions" # Evaluate expression and print result
node --check app.js # Validate syntax without executingDev & Watch Modes
node --watch app.js # Restart process automatically on file changes (Node 18+)
node --watch-path=./src app.js # Watch specific directoryRuntime Options
node --version # Print Node version
node -v
node --env-file=.env app.js # Load .env variables natively (Node 20+)
node --max-old-space-size=4096 app.js # Set V8 heap memory limit to 4GB
node --inspect app.js # Enable V8 inspector / Chrome DevTools
node --inspect-brk app.js # Break at script start and wait for debuggerNPM Basics
npm init -y # Create package.json with defaults
npm install # Install all dependencies in package.json
npm install pkg # Install dependency (production)
npm install -D pkg # Install dev dependency (--save-dev)
npm install -g pkg # Install package globally
npm uninstall pkg # Remove package
npm update # Update dependencies according to semver
npm outdated # Check for outdated dependencies
npm audit # Scan dependencies for vulnerabilities
npm audit fix # Automatically fix vulnerable packages
npm cache clean --force # Purge local NPM cache
npx pkg # Execute package binary directly (without global install)Quick Reference
| Command | Purpose |
|---|---|
node -v | Print installed Node version |
nvm use --lts | Switch to active LTS release |
node --watch app.js | Run with native file watcher |
node --env-file=.env app.js | Load environment variables from .env |
node --max-old-space-size=4096 | Allocate 4GB heap space |