NPM Package Manager
NPM cheat sheet covering package.json config, dependency management, scripts, publishing, workspaces, and CLI command reference.
Project Setup & Initialization
Initialize and configure new NPM projects
Create a new package.json file
# Interactive setup
npm init
# Quick setup with defaults
npm init -y
# Create with scope
npm init --scope=@mycompany
# Use create- starter
npm init react-app my-app
npm init vite@latest my-project
# Common init options
npm init -w packages/new-package # In workspace
npm init --workspace=packages/apiEssential fields in package.json
// package.json
{
"name": "my-project",
"version": "1.0.0",
"description": "Project description",
"main": "index.js",
"type": "module", // ES modules
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js",
"test": "jest",
"build": "webpack",
"lint": "eslint ."
},
"keywords": ["nodejs", "api"],
"author": "Your Name",
"license": "MIT",
"dependencies": {
"express": "^4.18.0"
},
"devDependencies": {
"nodemon": "^2.0.0",
"jest": "^29.0.0"
}
}Execute packages without installing globally
# Run package without installing
npx create-react-app my-app
npx eslint .
npx prettier --write .
# Run specific version
npx node@14 index.js
npx webpack@4 build
# Run from GitHub
npx github:user/repo
# Run local binary
npx mocha test/
# Clear npx cache
npx clear-npx-cachePackage Management
Install, update, and remove packages
Add dependencies to your project
# Install all dependencies
npm install
npm i # shorthand
# Install specific package
npm install express
npm install express@4.18.0 # specific version
# Install as dev dependency
npm install --save-dev nodemon
npm i -D jest # shorthand
# Install globally
npm install -g typescript
npm i -g pm2
# Install from git
npm install git+https://github.com/user/repo.git
npm install github:user/repoKeep dependencies up to date
# Check outdated packages
npm outdated
npm outdated -g # Global packages
# Update package
npm update express
npm update # Update all packages
# Update to latest (ignoring semver)
npm install express@latest
# Interactive update
npx npm-check -u
npx npm-check-updates
# Update package.json version ranges
npx npm-check-updates -u
npm install # Install updated versionsUninstall and clean up packages
# Remove package
npm uninstall express
npm remove express # Alias
npm rm express # Short alias
# Remove dev dependency
npm uninstall -D nodemon
# Remove global package
npm uninstall -g typescript
# Remove without updating package.json
npm uninstall express --no-save
# Clean cache
npm cache clean --force
npm cache verifyScripts & Task Running
Define and run npm scripts
Execute scripts defined in package.json
# Run scripts from package.json
npm run build
npm run test
npm run dev
# Special scripts (no 'run' needed)
npm start
npm test
npm stop
npm restart
# Pass arguments to scripts
npm run test -- --watch
npm run build -- --production
# List available scripts
npm run
# Run pre/post scripts
# pretest runs before test
# postbuild runs after buildConfigure NPM behavior and environment
# View config
npm config list
npm config get registry
# Set config
npm config set registry https://registry.npmjs.org/
npm config set save-exact true
# Delete config
npm config delete save-exact
# Edit config file
npm config edit
# Set for current command only
npm install --registry http://localhost:4873
# Environment variables
NODE_ENV=production npm run build
npm_config_production=true npm installPublishing & Versioning
Publish packages and manage versions
Semantic versioning and releases
# Bump version (updates package.json)
npm version patch # 1.0.0 -> 1.0.1
npm version minor # 1.0.0 -> 1.1.0
npm version major # 1.0.0 -> 2.0.0
# Specific version
npm version 1.2.3
# Prerelease versions
npm version prerelease # 1.0.0 -> 1.0.1-0
npm version prerelease --preid=beta # 1.0.1-beta.0
# With git tag
npm version patch -m "Release v%s"
# Without git tag
npm version patch --no-git-tag-versionPublish to NPM registry
# Login to registry
npm login
npm whoami # Verify login
# Publish package
npm publish
# Publish with tag
npm publish --tag beta
npm publish --tag next
# Publish scoped package publicly
npm publish --access public
# Dry run (see what would publish)
npm publish --dry-run
# Pack for inspection
npm pack # Creates .tgz fileSecurity & Maintenance
Keep projects secure and well-maintained
Find and fix security vulnerabilities
# Run security audit
npm audit
# Show detailed report
npm audit --json
# Fix automatically
npm audit fix
# Force fixes (may break)
npm audit fix --force
# Audit production only
npm audit --omit=dev
# Set audit level
npm audit --audit-level=moderateInspect and analyze packages
# View package info
npm view express
npm view express version # Latest version
npm view express versions # All versions
# List installed packages
npm ls
npm ls --depth=0 # Top level only
npm ls express # Find specific package
# Show package size
npm ls --prod --parseable | wc -l
# Find duplicate packages
npm dedupe --dry-run
npm find-dupes
# Package funding info
npm fund
npm fund expressWorkspaces & Advanced
Monorepo management with NPM workspaces
Manage multiple packages in monorepo
// Root package.json
{
"name": "my-monorepo",
"workspaces": [
"packages/*",
"apps/*"
]
}
# Install in workspace
npm install express -w packages/api
npm install -D jest --workspaces
# Run scripts in workspace
npm run build -w packages/core
npm run test --workspaces
# Execute in all workspaces
npm run build --workspaces --if-present
# Create new workspace
npm init -w packages/new-packageLink local packages for development
# Link package globally
cd my-package
npm link
# Use linked package
cd my-app
npm link my-package
# Unlink
npm unlink my-package
cd my-package && npm unlink
# List linked packages
npm ls -g --depth=0 --link
# Link specific package
npm link ../local-packageManage NPM cache and improve performance
# View cache info
npm cache ls
npm cache verify
# Clean cache
npm cache clean --force
# Offline install
npm install --offline
# Prefer offline
npm install --prefer-offline
# Cache location
npm config get cache
# Set cache folder
npm config set cache /path/to/cache