npm Cheat Sheet
Practical npm cheat sheet with setup steps, core workflows, debugging, and copy-paste examples.
npm terminal commands productivity
npm cheat sheet with real commands and snippets for setup, core workflows, debugging, and production-safe automation patterns. If you are working across tools, pair this with the Nix Cheat Sheet and sed Cheat Sheet.
Setup and Installation
Goal: Verify installation and CLI metadata
# Resolve command path from current shell
command -v npm
# Print installed version to confirm runtime
npm --version
# Read top-level help before using subcommands
npm --help
Goal: Install or upgrade the tool on a workstation
# Install package with Homebrew on macOS
brew install npm
# Upgrade to the latest available package
brew upgrade npm
# Re-check version after upgrade
npm --version
Goal: Run day-to-day commands
# Create package.json in current directory
npm init -y
# Install a runtime dependency
npm install axios
# Install a development dependency
npm install -D typescript
# Run package scripts
npm run build
Core Workflows
Goal: Capture command help for quick offline lookup
# Create docs folder for generated command references
mkdir -p docs/cli
# Write help output to a timestamped file
npm --help > docs/cli/npm-help.txt
# Search help output for a keyword
rg "config|auth|deploy" docs/cli/npm-help.txt
Goal: Wrap repetitive commands in a script
# Create scripts directory for local automation
mkdir -p scripts
# Write repeatable health-check script
cat > scripts/check-npm.sh <<'SH'
#!/usr/bin/env bash
set -euo pipefail
npm --version
npm --help >/dev/null
SH
# Make script executable and run it
chmod +x scripts/check-npm.sh && ./scripts/check-npm.sh
Configuration and Environment
Goal: Pin environment variables for predictable runs
# Define environment profile for local commands
export NPM_PROFILE=dev
# Persist profile in local shell configuration
echo 'export NPM_PROFILE=dev' >> ~/.zshrc
# Reload shell profile changes
source ~/.zshrc
Automation and CI
Goal: Add tool checks to CI pipeline
# Run lint/test/build in strict mode
set -euo pipefail
# Verify CLI exists before invoking workflow
command -v npm
# Exit quickly if command is unavailable
npm --version
Debugging and Troubleshooting
Goal: Collect diagnostics when commands fail
# Capture command output and exit code
npm --help > /tmp/npm-debug.log 2>&1; echo $?
# Inspect captured diagnostics
tail -n 80 /tmp/npm-debug.log
# Check current shell PATH entries
echo $PATH | tr ':' '\n'
Common Gotchas
- Pin npm versions in CI so local and pipeline behavior match.
- Check tool authentication context before running write or deploy commands.
- Prefer non-interactive flags in scripts to avoid stalled jobs.
- Capture stderr logs in CI artifacts for faster incident triage.
- Keep secrets in environment variables, not committed scripts.
Related Sheets
- Nix Cheat Sheet — daily Nix commands and production-ready examples.
- sed Cheat Sheet — daily sed commands and production-ready examples.
- SSH Cheat Sheet — daily SSH commands and production-ready examples.
Related Cheat Sheets
Nix Cheat Sheet
Practical Nix cheat sheet with setup steps, core workflows, debugging, and copy-paste examples.
sed Cheat Sheet
Practical sed cheat sheet with setup steps, core workflows, debugging, and copy-paste examples.
SSH Cheat Sheet
Practical SSH cheat sheet with setup steps, core workflows, debugging, and copy-paste examples.