Skip to content

npm Package Manager

Comprehensive npm (Node Package Manager) commands and workflows for JavaScript and Node.js development.

Installation & Setup

Install Node.js and npm

bash
# Using Node Version Manager (recommended)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
nvm install node
nvm use node

# Direct download from nodejs.org
# Or using package managers:
# macOS: brew install node
# Ubuntu: sudo apt install nodejs npm
# Windows: choco install nodejs

Basic Package Operations

CommandDescription
npm install package_nameInstall package locally
npm install -g package_nameInstall package globally
npm uninstall package_nameUninstall package
npm updateUpdate all packages
npm update package_nameUpdate specific package
npm listList installed packages
npm list -gList global packages

Project Management

Project Initialization

CommandDescription
npm initInitialize new project
npm init -yInitialize with defaults
npm init @scopeInitialize with scoped template
npm create package_nameCreate project with template

Dependency Management

CommandDescription
npm installInstall all dependencies
npm install --save packageInstall and save to dependencies
npm install --save-dev packageInstall and save to devDependencies
npm install --save-optional packageInstall as optional dependency
npm install --no-save packageInstall without saving

Package Information

CommandDescription
npm search keywordSearch for packages
npm info package_nameShow package information
npm view package_nameView package details
npm outdatedShow outdated packages
npm auditCheck for vulnerabilities
npm audit fixFix vulnerabilities

Scripts and Execution

Script Management

CommandDescription
npm run script_nameRun custom script
npm startRun start script
npm testRun test script
npm run buildRun build script
npm run devRun development script

Example package.json Scripts

json
{
  "scripts": {
    "start": "node server.js",
    "dev": "nodemon server.js",
    "build": "webpack --mode production",
    "test": "jest",
    "lint": "eslint src/",
    "format": "prettier --write src/"
  }
}

Version Management

Semantic Versioning

CommandDescription
npm version patchIncrement patch version
npm version minorIncrement minor version
npm version majorIncrement major version
npm version prereleaseCreate prerelease version

Package Installation by Version

CommandDescription
npm install package@1.2.3Install specific version
npm install package@latestInstall latest version
npm install package@nextInstall next/beta version
npm install package@^1.2.0Install compatible version

Configuration

npm Configuration

CommandDescription
npm config listShow configuration
npm config get keyGet configuration value
npm config set key valueSet configuration value
npm config delete keyDelete configuration

Common Configuration

SettingDescription
npm config set registry https://registry.npmjs.org/Set registry
npm config set init-author-name "Your Name"Set default author
npm config set init-license "MIT"Set default license
npm config set save-exact trueSave exact versions

.npmrc File

ini
# Project .npmrc
registry=https://registry.npmjs.org/
save-exact=true
engine-strict=true
fund=false
audit-level=moderate

# Global .npmrc (~/.npmrc)
init-author-name=Your Name
init-author-email=your.email@example.com
init-license=MIT

Publishing

Package Publishing

CommandDescription
npm loginLogin to npm registry
npm whoamiCheck logged in user
npm publishPublish package
npm publish --access publicPublish scoped package publicly
npm unpublish package@versionUnpublish specific version

Publishing Workflow

bash
# 1. Update version
npm version patch

# 2. Build package
npm run build

# 3. Test package
npm test

# 4. Publish
npm publish

# 5. Tag release
git tag v1.0.0
git push origin v1.0.0

Advanced Usage

Workspaces (npm 7+)

json
{
  "name": "my-monorepo",
  "workspaces": [
    "packages/*",
    "apps/*"
  ]
}
CommandDescription
npm install --workspacesInstall all workspace dependencies
npm run test --workspacesRun tests in all workspaces
npm run build --workspace=package-aRun command in specific workspace

Package Linking

CommandDescription
npm linkCreate global link
npm link package_nameLink to global package
npm unlink package_nameUnlink package

Cache Management

CommandDescription
npm cache verifyVerify cache integrity
npm cache clean --forceClear cache
npm cache lsList cached packages

Security

Security Auditing

CommandDescription
npm auditCheck for vulnerabilities
npm audit --audit-level highCheck high severity only
npm audit fixFix vulnerabilities automatically
npm audit fix --forceForce fix (may break changes)

Package Verification

CommandDescription
npm packCreate tarball
npm pack --dry-runShow what would be packed
npm install package.tgzInstall from tarball

Performance Optimization

Faster Installation

bash
# Use npm ci for CI/CD
npm ci

# Parallel installation
npm install --prefer-offline

# Skip optional dependencies
npm install --no-optional

# Production only
npm install --production

Alternative Package Managers

ManagerInstallationBenefits
Yarnnpm install -g yarnFaster, deterministic
pnpmnpm install -g pnpmDisk space efficient
Buncurl -fsSL https://bun.sh/install | bashExtremely fast

Troubleshooting

Common Issues

ProblemSolution
Permission errorsUse nvm or fix permissions
Package conflictsDelete node_modules and reinstall
Outdated npmnpm install -g npm@latest
Registry issuesnpm config set registry https://registry.npmjs.org/

Debugging

CommandDescription
npm doctorCheck npm environment
npm lsCheck dependency tree
npm ls --depth=0Show top-level dependencies
npm why package_nameShow why package is installed

Reset and Clean Install

bash
# Clean install
rm -rf node_modules package-lock.json
npm install

# Clear npm cache
npm cache clean --force

# Reset npm configuration
npm config edit

Best Practices

Package.json Management

json
{
  "name": "my-project",
  "version": "1.0.0",
  "description": "Project description",
  "main": "index.js",
  "engines": {
    "node": ">=16.0.0",
    "npm": ">=8.0.0"
  },
  "dependencies": {
    "express": "^4.18.0"
  },
  "devDependencies": {
    "jest": "^29.0.0",
    "eslint": "^8.0.0"
  },
  "peerDependencies": {
    "react": ">=16.0.0"
  }
}

Development Workflow

  1. Version Pinning: Use exact versions for critical dependencies
  2. Lock Files: Commit package-lock.json to version control
  3. Security: Regular security audits with npm audit
  4. Testing: Test before publishing with npm pack
  5. Documentation: Maintain comprehensive README

Performance

  1. CI/CD: Use npm ci in continuous integration
  2. Caching: Leverage npm cache in CI environments
  3. Selective Installation: Use --production for production builds
  4. Alternative Managers: Consider yarn or pnpm for large projects
  5. Registry: Use private registry for internal packages