npm & yarn Cheat Sheet

Package management commands for Node.js. Installing, updating, removing packages, running scripts, versioning, and publishing.

Project Init

npmyarnDescription
npm inityarn initCreate package.json interactively
npm init -yyarn init -yCreate with defaults (skip prompts)
npm create vite@latestyarn create viteCreate project from initializer
npx create-react-app appyarn dlx create-react-app appRun package without installing

Installing Packages

npmyarnDescription
npm installyarnInstall all dependencies from package.json
npm install pkgyarn add pkgAdd package as dependency
npm install pkg@2.0yarn add pkg@2.0Install specific version
npm install -D pkgyarn add -D pkgAdd as dev dependency
npm install -g pkgyarn global add pkgInstall globally
npm ciyarn install --frozen-lockfileClean install (CI/CD, exact lockfile)
npm install --legacy-peer-depsIgnore peer dependency conflicts
Tip: Use npm ci (or yarn --frozen-lockfile) in CI/CD pipelines. It's faster and ensures exact versions from the lock file are used.

Removing & Updating

npmyarnDescription
npm uninstall pkgyarn remove pkgRemove package
npm uninstall -g pkgyarn global remove pkgRemove global package
npm updateyarn upgradeUpdate all packages
npm update pkgyarn upgrade pkgUpdate specific package
npm outdatedyarn outdatedList outdated packages
npx npm-check-updates -uUpdate all versions in package.json
npm audityarn auditCheck for vulnerabilities
npm audit fixAuto-fix vulnerabilities
npm dedupeyarn dedupeRemove duplicate packages

Scripts

npmyarnDescription
npm run devyarn devRun custom script
npm startyarn startRun "start" script
npm testyarn testRun "test" script
npm run buildyarn buildRun "build" script
npx pkgyarn dlx pkgExecute package binary
// package.json scripts section
{
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview",
    "lint": "eslint src/",
    "test": "vitest",
    "preinstall": "npx only-allow pnpm"   // lifecycle hook
  }
}
Tip: npm start, npm test, and npm stop don't need the run keyword. All other scripts need npm run <script>.

Package Info

npmDescription
npm listList installed packages (tree)
npm list --depth=0List top-level packages only
npm list -g --depth=0List global packages
npm info pkgShow package info from registry
npm info pkg versionsList all available versions
npm search keywordSearch npm registry
npm docs pkgOpen package documentation
npm repo pkgOpen package repository
npm explain pkgShow why a package is installed

Versioning

RangeMeaningExample
1.2.3Exact versionOnly 1.2.3
^1.2.3Compatible (same major)>=1.2.3 <2.0.0
~1.2.3Approximately (same minor)>=1.2.3 <1.3.0
>=1.2.3Greater than or equal>=1.2.3
1.xAny minor/patch in major 1>=1.0.0 <2.0.0
*Any versionLatest
latestLatest published versionTag-based
# Bump version
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
SemVer: MAJOR.MINOR.PATCH — Major = breaking changes, Minor = new features (backward compatible), Patch = bug fixes.

Publishing

CommandDescription
npm loginLog in to npm registry
npm whoamiShow current logged-in user
npm publishPublish package to registry
npm publish --access publicPublish scoped package as public
npm unpublish pkg@1.0.0Unpublish specific version
npm deprecate pkg "message"Mark package as deprecated
npm packCreate tarball without publishing
npm linkSymlink package for local development

Configuration

CommandDescription
npm config listShow current config
npm config set key valueSet config value
npm config get registryGet registry URL
npm cache clean --forceClear npm cache
npm root -gShow global packages directory
# .npmrc (project or user config)
registry=https://registry.npmjs.org/
save-exact=true
engine-strict=true

pnpm (Alternative)

pnpmEquivalent
pnpm installnpm install / yarn
pnpm add pkgnpm install pkg / yarn add pkg
pnpm add -D pkgnpm install -D pkg
pnpm remove pkgnpm uninstall pkg
pnpm run devnpm run dev
pnpm dlx pkgnpx pkg
Tip: pnpm uses a content-addressable store and hard links, making it faster and more disk-efficient than npm. It also enforces strict dependency isolation.

package.json Key Fields

FieldDescription
namePackage name (lowercase, no spaces)
versionSemVer version string
descriptionShort description
mainEntry point for CommonJS
moduleEntry point for ES modules
type"module" for ESM, "commonjs" for CJS
scriptsRunnable commands
dependenciesProduction dependencies
devDependenciesDevelopment-only dependencies
peerDependenciesRequired by host package
enginesRequired Node.js / npm versions
filesFiles to include when publishing
privatetrue prevents accidental publishing
licenseSPDX license identifier