>_cat nodejs.md|Web & App Servers
Index

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 nodejs

RHEL / CentOS / Fedora

# Via NodeSource
curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash -
sudo dnf install -y nodejs

Arch Linux

sudo pacman -S nodejs npm

macOS

brew install node

NVM (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 ~/.bashrc

Manage 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 v18

Inspection

nvm ls                       # List locally installed versions
nvm ls-remote                # List available remote versions
nvm current                  # Print active version

Node.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 executing

Dev & Watch Modes

node --watch app.js          # Restart process automatically on file changes (Node 18+)
node --watch-path=./src app.js # Watch specific directory

Runtime 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 debugger

NPM 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

CommandPurpose
node -vPrint installed Node version
nvm use --ltsSwitch to active LTS release
node --watch app.jsRun with native file watcher
node --env-file=.env app.jsLoad environment variables from .env
node --max-old-space-size=4096Allocate 4GB heap space