NPM & Yarn Commands Cheatsheet
Complete side-by-side reference for npm and Yarn — from initializing projects and managing dependencies to publishing packages, workspaces, and cache management. Bookmark and never look up a command again.
Init & Setup
npm inityarn initnpm init -yyarn init -ynpm init @scopeyarn create @scopenpx create-react-app my-appyarn dlx create-react-app my-appnpx <package>yarn dlx <package>npm installyarn / yarn installnpm ciyarn install --frozen-lockfilenpm install --legacy-peer-depsyarn install --ignore-enginesnpm set init-author-name "Name"yarn config set init-author-name "Name"npm set init-license MITyarn config set init-license MITDependencies
npm install <pkg>yarn add <pkg>npm install <pkg> --save-devyarn add <pkg> --devnpm install <pkg> --save-exactyarn add <pkg> --exactnpm install <pkg>@<version>yarn add <pkg>@<version>npm install <pkg> --globalyarn global add <pkg>npm uninstall <pkg>yarn remove <pkg>npm uninstall <pkg> --globalyarn global remove <pkg>npm updateyarn upgradenpm update <pkg>yarn upgrade <pkg>npm outdatedyarn outdatednpm audityarn auditnpm audit fixyarn audit (manual fix)npm fundyarn fundnpm install <pkg>@latestyarn add <pkg>@latestnpm install <git-url>yarn add <git-url>npm install ./local-pkgyarn add file:./local-pkgnpm dedupeyarn dedupeScripts
npm run <script>yarn <script> / yarn run <script>npm startyarn startnpm testyarn testnpm run buildyarn buildnpm run lintyarn lintnpm run devyarn devnpm run <script> -- --flagyarn <script> --flagnpm run envyarn run envnpm restartyarn run restartnpm stopyarn run stoppre<script> / post<script>pre<script> / post<script>Publishing
npm loginyarn loginnpm logoutyarn logoutnpm whoamiyarn whoami (npm whoami)npm publishyarn publishnpm publish --access publicyarn publish --access publicnpm publish --tag nextyarn publish --tag nextnpm packyarn packnpm version <major|minor|patch>yarn version --<major|minor|patch>npm version prerelease --preid=betayarn version --prereleasenpm deprecate <pkg>@<ver> "msg"npm deprecate <pkg>@<ver> "msg"npm unpublish <pkg>@<ver>npm unpublish <pkg>@<ver>npm dist-tag add <pkg>@<ver> latestyarn tag add <pkg>@<ver> latestnpm owner add <user> <pkg>yarn owner add <user> <pkg>Registry & Config
npm config listyarn config listnpm config get <key>yarn config get <key>npm config set <key> <value>yarn config set <key> <value>npm config delete <key>yarn config delete <key>npm config set registry <url>yarn config set registry <url>npm config set //registry/:_authToken=<t>yarn config set npmAuthToken <t>.npmrc (project or user).yarnrc.yml (Yarn Berry)npm config set save-exact trueyarn config set save-prefix ""npm config set engine-strict trueyarn config set enableStrictEngine truenpm token createnpm token createnpm token listnpm token listWorkspace & Monorepo
"workspaces": ["packages/*"]"workspaces": ["packages/*"]npm install (from root)yarn install (from root)npm run <script> -w <pkg>yarn workspace <pkg> <script>npm run <script> --workspacesyarn workspaces foreach run <script>npm install <dep> -w <pkg>yarn workspace <pkg> add <dep>npm ls --all --workspacesyarn workspaces listnpm exec -w <pkg> -- <cmd>yarn workspace <pkg> exec <cmd>npm publish --workspacesyarn workspaces foreach npm publishnpx lerna initnpx lerna initnpx lerna run buildnpx lerna run buildnpx lerna versionnpx lerna versionCache & Cleanup
npm cache clean --forceyarn cache cleannpm cache verifyyarn cache listnpm cache lsyarn cache list --pattern <pkg>npm pruneyarn autoclean --forcerm -rf node_modules && npm installrm -rf node_modules && yarnnpm cache clean --force && rm -rf node_modulesyarn cache clean && rm -rf node_modulesnpm config get cacheyarn cache dirInfo & Inspection
npm lsyarn listnpm ls --allyarn list --allnpm ls <pkg>yarn list --pattern <pkg>npm info <pkg>yarn info <pkg>npm view <pkg> versionsyarn info <pkg> versionsnpm view <pkg> dependenciesyarn info <pkg> dependenciesnpm why <pkg>yarn why <pkg>npm explain <pkg>yarn why <pkg>npm doctor(no equivalent)npm binyarn binnpm bin --globalyarn global binnpm rootyarn root (custom)npm prefix(pwd)npm -vyarn -vnpm help <command>yarn help <command>npm search <term>npm search <term>FAQ
What are the main differences between npm, Yarn, and pnpm?
npm is the default Node.js package manager — universal and well-supported. Yarn (by Facebook) introduced lock files, workspaces, and parallel installs before npm caught up. Yarn Berry (v2+) uses Plug'n'Play for zero node_modules. pnpm uses a content-addressable store with hard links, saving disk space and enforcing strict dependency isolation. All three share the same registry. Choose npm for simplicity, Yarn for workspaces/PnP features, and pnpm for speed and disk efficiency in monorepos.
What are lock files and why are they important?
Lock files (package-lock.json for npm, yarn.lock for Yarn, pnpm-lock.yaml for pnpm) record the exact resolved version of every installed dependency. They ensure that every developer and CI environment gets identical node_modules. Always commit lock files to version control. Without them, different installs can resolve different versions, leading to 'works on my machine' bugs.
How do peer dependencies work?
Peer dependencies declare that your package is compatible with a specific version of another package, but expects the consuming project to provide it. For example, a React component library might list react as a peerDependency. npm 7+ auto-installs peer deps (with warnings on conflicts). Yarn and pnpm show warnings but don't auto-install. Use --legacy-peer-deps (npm) to bypass strict resolution if you hit conflicts.
What is the difference between npx and yarn dlx?
Both execute a package binary without permanently installing it. npx (bundled with npm 5.2+) first checks local node_modules/.bin, then global installs, then downloads temporarily. yarn dlx (Yarn Berry) always downloads to a temp folder and runs — it never uses locally installed packages. Both are perfect for scaffolding tools (create-react-app, create-next-app) and one-off utilities you don't want cluttering your global installs.