Categories
NodeJS

Essential Node.js, npm, and nvm Commands Reference

Node.js Commands

Basic

# Run a JavaScript file
node app.js

# Start the REPL (interactive shell)
node

# Check Node.js version
node -v

# Run with debugging
node --inspect app.js

# Run with V8 inspector for Chrome DevTools
node --inspect-brk app.js

Advanced

# Run with specific memory allocation
node --max-old-space-size=4096 app.js

# Check for syntax errors without executing
node --check app.js

# Show all deprecation warnings
node --trace-deprecation app.js

# Run with experimental features
node --experimental-modules app.js

# Print environment variables
node -p "process.env"

# Run with native profiling
node --prof app.js

npm Commands

Package Installation

# Install all dependencies from package.json
npm install

# Install a specific package locally
npm install package-name

# Install a specific package globally
npm install -g package-name

# Install as a development dependency
npm install --save-dev package-name

# Install exact version (no ^ or ~)
npm install --save-exact package-name

# Install a specific version
npm install package-name@1.2.3

Package Management

# Uninstall a package locally
npm uninstall package-name

# Uninstall a package globally
npm uninstall -g package-name

# Update all packages
npm update

# Update a specific package
npm update package-name

# Check outdated packages
npm outdated

# List installed packages
npm list

# List top-level installed packages
npm list --depth=0

# List global packages
npm list -g --depth=0

Project Management

# Initialize a new project
npm init

# Initialize with defaults (skips questions)
npm init -y

# Run a script defined in package.json
npm run script-name

# Run the start script
npm start

# Run the test script
npm test

# Run the build script
npm run build

Package Publishing

# Create a tarball
npm pack

# Publish a package to npm registry
npm publish

# Tag a package with a specific tag
npm publish --tag beta

# Increment patch version (1.0.0 → 1.0.1)
npm version patch

# Increment minor version (1.0.0 → 1.1.0)
npm version minor

# Increment major version (1.0.0 → 2.0.0)
npm version major

npm Configuration

# Set npm config value
npm config set key value

# Get npm config value
npm config get key

# List all config settings
npm config list

# Edit config file with default editor
npm config edit

# Set init defaults
npm config set init.author.name "Your Name"
npm config set init.author.email "your.email@example.com"

Dependency Analysis

# View dependency tree
npm ls

# Check for security vulnerabilities
npm audit

# Fix security vulnerabilities automatically
npm audit fix

# Get info about a package
npm info package-name

# View package.json of a dependency
npm view package-name

nvm Commands

Installation & Version Management

# List available Node.js versions
nvm ls-remote

# Install latest LTS Node.js version
nvm install --lts

# Install specific Node.js version
nvm install 16.14.0

# Use a specific Node.js version
nvm use 16.14.0

# Set default Node.js version
nvm alias default 16.14.0

# List installed versions
nvm ls

# Run a command with a specific version
nvm exec 14.17.0 node app.js

Project Management

# Use .nvmrc file in current directory
nvm use

# Create .nvmrc file with current version
node -v > .nvmrc

# Install version from .nvmrc
nvm install

System Management

# Show current Node.js version
nvm current

# Show path to executable
nvm which 16.14.0

# Uninstall a version
nvm uninstall 14.17.0

# Show nvm version
nvm --version

# Clear nvm cache
nvm cache clear

Advanced nvm Usage

# Install with custom arch (32-bit)
nvm install --arch=x86 14.17.0

# Use with specific environment variables
nvm use --silent 16.14.0

# Run Node.js with specific flags
nvm run 16.14.0 --max-old-space-size=4096 app.js

# Install version for specific project
cd project-directory && nvm install

Combined Workflows

Project Setup

# Set up a new project with specific Node.js version
nvm install 16.14.0
nvm use 16.14.0
npm init -y
npm install express mongoose dotenv

Development Workflow

# Start development server with automatic restart
npm install --save-dev nodemon
# Add to package.json: "dev": "nodemon app.js"
npm run dev

Production Deployment

# Install only production dependencies
npm install --production

# Run in production mode
NODE_ENV=production node app.js

Performance Testing

# Run with heap snapshot
node --heapsnapshot-signal=SIGUSR2 app.js

# Run with CPU profiling
node --prof app.js
# After running, analyze with:
node --prof-process isolate-*.log > profile.txt

Package Maintenance

# Update npm itself
npm install -g npm@latest

# Update all dependencies to latest versions
npx npm-check-updates -u
npm install