npm provides various commands for managing packages and projects. Mastering these commands is crucial for efficient JavaScript development.
Package Management Commands
Installing Packages
bash# Install latest version npm install <package> # Install specific version npm install <package>@<version> # Install multiple packages npm install <package1> <package2> <package3> # Install as production dependency npm install <package> --save-prod npm install <package> -P # Install as development dependency npm install <package> --save-dev npm install <package> -D # Install as optional dependency npm install <package> --save-optional npm install <package> -O # Install as peer dependency npm install <package> --save-peer # Global installation npm install <package> --global npm install <package> -g # Install exact version npm install <package> --save-exact npm install <package> -E # Force reinstall npm install <package> --force
Updating Packages
bash# Update all packages (following ranges in package.json) npm update # Update specific package npm update <package> # Update to latest major version npm update <package> --latest # Check for outdated packages npm outdated # Check if specific package is outdated npm outdated <package>
Uninstalling Packages
bash# Uninstall package npm uninstall <package> # Uninstall multiple packages npm uninstall <package1> <package2> # Uninstall global package npm uninstall <package> --global npm uninstall <package> -g # Uninstall and remove from package.json npm uninstall <package> --save # Uninstall dev dependency npm uninstall <package> --save-dev
Information Query Commands
View Package Information
bash# View basic package information npm info <package> # View specific field of package npm info <package> version npm info <package> description npm info <package> author npm info <package> homepage # View all versions of package npm info <package> versions # View package dependencies npm info <package> dependencies # View package dev dependencies npm info <package> devDependencies # View package peer dependencies npm info <package> peerDependencies # View package publication time npm info <package> time # View package download count npm info <package> downloads # View package maintainers npm info <package> maintainers # View package keywords npm info <package> keywords
View Installed Packages
bash# View all installed packages npm list # View top-level dependencies npm list --depth=0 # View specific package npm list <package> # View globally installed packages npm list --global # View global top-level dependencies npm list --global --depth=0 # Output in JSON format npm list --json # Show only production dependencies npm list --production # Show only development dependencies npm list --dev # Show package size npm list --long
Search Packages
bash# Search for packages npm search <keyword> # Search and limit results npm search <keyword> --long # Search in JSON format npm search <keyword> --json # Search for packages by specific maintainer npm search <keyword> --author <author> # Search for packages with specific keywords npm search <keyword> --keywords <keywords>
Project Management Commands
Initialize Project
bash# Initialize new project (interactive) npm init # Initialize with default values npm init -y npm init --yes # Initialize with specific configuration npm init --scope=@mycompany # Create specific type of package npm init react-app my-app npm init vue-app my-app
Run Scripts
bash# Run script npm run <script> # Run start script (shorthand) npm start # Run stop script (shorthand) npm stop # Run test script (shorthand) npm test # Run restart script (shorthand) npm restart # List all scripts npm run # Pass arguments to script npm run <script> -- --arg1 --arg2 # Run in silent mode npm run <script> --silent npm run <script> -s
Version Management
bash# Update patch version (1.0.0 -> 1.0.1) npm version patch # Update minor version (1.0.0 -> 1.1.0) npm version minor # Update major version (1.0.0 -> 2.0.0) npm version major # Update prerelease version npm version prerelease npm version prerelease --preid beta npm version prerelease --preid alpha # Update to specific version npm version 1.2.3 # Create Git tag npm version patch -m "Bump to version %s" # Don't create Git tag npm version patch --no-git-tag-version
Publishing Commands
Publish Package
bash# Publish package npm publish # Publish with specific tag npm publish --tag beta npm publish --tag next # Publish as public package (scoped package) npm publish --access public # Publish as restricted package (scoped package) npm publish --access restricted # Publish specific directory npm publish <directory> # Dry run before publishing npm publish --dry-run # Publish with provenance npm publish --provenance
Unpublish
bash# Unpublish specific version npm unpublish <package>@<version> # Unpublish entire package (use with caution) npm unpublish <package> # Force unpublish npm unpublish <package>@<version> --force
Deprecate Version
bash# Deprecate specific version npm deprecate <package>@<version> "This version is deprecated" # Deprecate all versions npm deprecate <package> "This package is deprecated"
Configuration Commands
View Configuration
bash# View all configuration npm config list # View specific configuration npm config get <key> # View user configuration npm config list --user # View global configuration npm config list --global # View project configuration npm config list --project # Edit configuration file npm config edit # View configuration file location npm config get userconfig npm config get globalconfig
Set Configuration
bash# Set configuration npm config set <key> <value> # Set global configuration npm config set <key> <value> --global # Delete configuration npm config delete <key> # Delete global configuration npm config delete <key> --global
Common Configuration
bash# Set registry npm config set registry https://registry.npmmirror.com # Set cache directory npm config set cache /path/to/cache # Set prefix npm config set prefix /usr/local # Set log level npm config set loglevel info # Set proxy npm config set https-proxy http://proxy.example.com:8080 # Set strict SSL npm config set strict-ssl true # Set save prefix npm config set save-prefix ^ # Set exact save npm config set save-exact true
Cache Commands
Manage Cache
bash# Verify cache npm cache verify # Clean cache npm cache clean # Force clean cache npm cache clean --force # Add to cache npm cache add <package>@<version> # View cache directory npm config get cache
Security Commands
Security Audit
bash# Run security audit npm audit # Display audit results in JSON format npm audit --json # Audit only production dependencies npm audit --production # Audit only development dependencies npm audit --dev # Automatically fix vulnerabilities npm audit fix # Force fix vulnerabilities npm audit fix --force # Fix only production dependency vulnerabilities npm audit fix --production # Show vulnerability details npm audit --audit-level=low npm audit --audit-level=moderate npm audit --audit-level=high npm audit --audit-level=critical
Advanced Commands
Workspace Commands
bash# Run command in all workspaces npm run <script> -ws npm run <script> --workspaces # Run command in specific workspace npm run <script> --workspace=<workspace> # Install to all workspaces npm install -ws # Add to specific workspace npm add <package> --workspace=<workspace>
Exec Commands
bash# Execute binary from package npm exec <package> # Execute specific package npm exec --package=<package> <command> # Equivalent to npx npx <package> # Execute and pass arguments npx <package> --arg1 --arg2 # Use specific version npx <package>@<version> # Ignore local installation npx --ignore-existing <package> # Use cache npx --yes <package> npx -y <package>
Query Commands
bash# Query dependencies npm query ":root" # Query all outdated packages npm query ":outdated" # Query all development dependencies npm query ":dev" # Query dependencies of specific package npm query "lodash > *" # Query packages in specific path npm query "#./packages/*"
Diff Commands
bash# Compare current installation with package.json npm diff # Compare specific package npm diff <package> # Compare two versions npm diff <package>@1.0.0 <package>@2.0.0 # Compare with git npm diff --git
Other Useful Commands
Fund Commands
bash# View project funding sources npm fund # View specific package funding sources npm fund <package> # Open funding link npm fund --which=<package>
Organization Commands
bash# Create organization npm org create <organization> # Add member npm org add <user> <organization> # Remove member npm org rm <user> <organization> # List members npm org ls <organization> # Set role npm org set <user> <role> <organization>
Access Commands
bash# Set package access to public npm access public <package> # Set package to restricted access npm access restricted <package> # Add collaborator to package npm access add <user> <package> # Remove collaborator from package npm access rm <user> <package> # List package collaborators npm access ls <package> # View package access permissions npm access status <package>
Login and Logout
bash# Login npm login npm adduser # Logout npm logout # View current user npm whoami # View user information npm profile get # Update user information npm profile set <key> <value> # Change password npm profile set password
Team Commands
bash# Create team npm team create <team> <organization> # Add member to team npm team add <user> <team> # Remove member from team npm team rm <user> <team> # List team members npm team ls <team> # Delete team npm team destroy <team>
Best Practices
1. Use npm ci Instead of npm install
bash# Use npm ci in CI environment npm ci
2. Use Exact Versions
bash# Use exact versions in production npm install <package> --save-exact
3. Regularly Update Dependencies
bash# Check for outdated packages npm outdated # Update packages npm update
4. Regularly Run Security Audits
bash# Run security audit npm audit # Fix vulnerabilities npm audit fix
5. Use .npmrc Configuration
bash# Project-level configuration echo "registry=https://registry.npmmirror.com" > .npmrc
Mastering these npm commands can significantly improve development efficiency and project management capabilities.