1  Node.js & npm Command Cheatsheet

1.1 Project Initialization & Setup

1.1.1 Starting a New Project

# Initialize new project (similar to 'flutter create' but for Node.js)
npm init                    # Interactive project setup
npm init -y                # Quick setup with defaults
npm init @scope/template   # Use specific template (like create-react-app)

# Create package.json with specific settings
npm init --name="my-project" --version="1.0.0"

1.2 Package Management

1.2.1 Installing Packages

# Install dependencies (similar to 'pub get' in Flutter)
npm install                 # Install all dependencies from package.json
npm install <package>       # Install specific package
npm install <package>@1.2.3 # Install specific version

# Development vs Production dependencies
npm install <package> --save-dev    # Dev dependency (like dev_dependencies in pubspec.yaml)
npm install <package> --save-prod   # Production dependency (default)

# Global installations (system-wide tools)
npm install -g <package>    # Install globally (like pub global activate)

# Alternative installation methods
npm install <package> --no-save     # Install without adding to package.json
npm install --production            # Install only production dependencies

1.2.2 Package Information & Management

# View installed packages
npm list                    # Show dependency tree
npm list --depth=0         # Show only top-level packages
npm list -g                # Show global packages
npm list <package>         # Check if specific package is installed

# Package information
npm info <package>         # Show package details
npm outdated              # Show outdated packages
npm audit                 # Security vulnerability check
npm audit fix             # Automatically fix vulnerabilities

1.2.3 Updating & Removing Packages

# Update packages
npm update                 # Update all packages
npm update <package>       # Update specific package

# Remove packages
npm uninstall <package>    # Remove package and update package.json
npm uninstall <package> --save-dev  # Remove dev dependency
npm uninstall -g <package> # Remove global package

1.3 Running Scripts & Applications

1.3.1 Script Execution

# Run scripts defined in package.json (like 'flutter run')
npm run <script-name>      # Run custom script
npm start                  # Run 'start' script (usually starts the app)
npm test                   # Run 'test' script
npm run build             # Run 'build' script (common for production builds)
npm run dev               # Run 'dev' script (common for development server)

# Run Node.js files directly
node <filename.js>         # Execute JavaScript file
node -e "console.log('Hello')"  # Execute inline JavaScript

1.3.2 Development Tools

# Watch for file changes (development)
npx nodemon <filename.js>  # Auto-restart on file changes
npm run dev               # Often configured to use nodemon or similar

# REPL (Read-Eval-Print Loop)
node                      # Start interactive Node.js session

1.4 Package Runners & Tools

1.4.1 NPX (Node Package Execute)

# Run packages without installing globally
npx <package>             # Run package temporarily
npx create-react-app my-app  # Use package generators
npx eslint .              # Run linting tools
npx prettier --write .    # Format code

# Run local packages
npx <local-package>       # Run locally installed package

1.5 Cache & Configuration Management

1.5.1 Cache Operations

# Cache management
npm cache verify          # Verify cache integrity
npm cache clean --force   # Clear npm cache (similar to 'flutter clean')

# Configuration
npm config list           # Show all configuration
npm config get <key>      # Get specific config value
npm config set <key> <value>  # Set config value

1.6 Version & Registry Management

1.6.1 Version Information

# Version checks
node --version            # Check Node.js version
npm --version            # Check npm version
npm version              # Show version info for current project

# Version bumping (in your project)
npm version patch        # Increment patch version (1.0.0 → 1.0.1)
npm version minor        # Increment minor version (1.0.0 → 1.1.0)  
npm version major        # Increment major version (1.0.0 → 2.0.0)

1.6.2 Registry Operations

# Package registry
npm search <keyword>      # Search for packages
npm view <package>       # View package details
npm publish              # Publish your package to npm registry
npm unpublish <package>  # Unpublish package (use carefully!)

# Registry configuration
npm config set registry <url>  # Set custom registry
npm config get registry        # Check current registry

1.7 Environment & Process Management

1.7.1 Environment Variables

# Set environment variables for Node.js
NODE_ENV=production npm start    # Set environment for single command
export NODE_ENV=development      # Set for session (in shell)

# Common environment patterns
npm run build:prod              # Often sets NODE_ENV=production
npm run dev                     # Often sets NODE_ENV=development

1.7.2 Process Management

# Process control
node --inspect <file.js>        # Start with debugger
node --max-old-space-size=4096  # Increase memory limit
node --trace-warnings           # Show stack traces for warnings

# Kill processes
killall node                    # Kill all Node.js processes (macOS/Linux)
pkill -f node                   # Alternative way to kill Node processes

1.8 Common Development Workflow

1.8.1 Typical Project Setup Flow

# 1. Create new project directory
mkdir my-project && cd my-project

# 2. Initialize npm project
npm init -y

# 3. Install dependencies
npm install express            # Example: web framework
npm install --save-dev nodemon # Example: development tool

# 4. Create main file
echo "console.log('Hello Node.js!');" > index.js

# 5. Add scripts to package.json (edit manually or via npm)
npm pkg set scripts.start="node index.js"
npm pkg set scripts.dev="nodemon index.js"

# 6. Run the application
npm run dev                    # For development
npm start                      # For production

1.9 Troubleshooting Commands

1.9.1 Common Issues Resolution

# Clear everything and reinstall
rm -rf node_modules package-lock.json
npm install                    # Fresh installation

# Permission issues (avoid using sudo with npm)
npm config set prefix ~/.npm-global  # Set global prefix to user directory

# Check for issues
npm doctor                     # Run npm health check
npm ls                         # Check for dependency issues
npm audit                      # Security check

1.10 Key Differences from Flutter/Dart

Concept Flutter/Dart Node.js/npm
Package file pubspec.yaml package.json
Lock file pubspec.lock package-lock.json
Install deps pub get npm install
Run app flutter run npm start or node file.js
Global tools pub global activate npm install -g
Clean cache flutter clean npm cache clean --force
Dev dependencies dev_dependencies: npm install --save-dev

1.11 Pro Tips

  • Use npm ci instead of npm install in CI/CD environments for faster, reliable installs
  • Create .nvmrc file to specify Node.js version for your project
  • Use npm run without arguments to list all available scripts
  • Add node_modules/ to your .gitignore file (never commit dependencies)
  • Use npx to run tools without global installation
  • Check package-lock.json into version control for reproducible builds