npm & Yarn Cheatsheet
Every essential npm and Yarn command: project setup, installing packages, scripts, workspaces, publishing, and audits, with syntax and real use cases.82 commands · 6 sections
npm and Yarn are the package managers of the JavaScript ecosystem. This cheatsheet covers the commands you run daily: initializing projects, installing and updating dependencies, running scripts, workspaces and monorepos, publishing packages, and auditing for vulnerabilities.
npm commands work for both; where Yarn differs, both forms are shown.
Project Setup10
npm initnpm init -ynpm installnpm ciyarn init -yyarn installyarn set version stablenpm pkg set name="myapp" version="1.0.0"npm pkg delete scripts.testnpm pkg get scriptsInstalling Packages17
npm install <pkg>npm install -D <pkg>npm install <pkg>@latestnpm install <pkg>@1.2.3npm install <pkg>@^1.0.0npm install -g <pkg>npm install --save-exact <pkg>npm uninstall <pkg>npm uninstall -D <pkg>npm lsnpm ls --depth=0npm ls <pkg>yarn add <pkg>yarn add -D <pkg>yarn remove <pkg>npm install <github:user/repo>npm install <pkg> --no-saveScripts & Running13
npm run <script>npm run devnpm testnpm startnpm run <script> -- --flagnpm run --silent <script>npm run <script> --if-presentnpx <cmd>npx <pkg>@latestnpx --yes <pkg>npm exec <cmd>yarn run <script>yarn dlx <cmd>Workspaces & Monorepos10
"workspaces": ["packages/*"]npm install (with workspaces)npm run <script> -w <workspace>npm run <script> --workspacesnpm install <pkg> -w packages/utilsnpm ls --workspacesyarn workspaces listyarn workspace packages/utils testnpm packnpm pack --dry-runPublishing & Registry16
npm loginnpm whoaminpm publishnpm publish --access publicnpm publish --tag betanpm version patchnpm version minor / majornpm version prerelease --preid=betanpm deprecate <pkg>@<version> "message"npm unpublish <pkg>@<version>npm dist-tag ls <pkg>npm dist-tag add <pkg>@1.0.0 latestnpm config set registry https://npm.pkg.github.com/npm view <pkg>npm view <pkg> versionsnpm search <term>Maintenance & Audits16
npm outdatednpm updatenpm update <pkg>npm auditnpm audit fixnpm audit fix --forcenpm audit --jsonnpm dedupenpm cache clean --forcenpm cache verifynpm prunenpm prune --productionnpm explain <pkg>npm doctornpm root -gnpm run envnpm & Yarn Cheatsheet
Every essential npm and Yarn command: project setup, installing packages, scripts, workspaces, publishing, and audits, with syntax and real use cases.
npm and Yarn are the package managers of the JavaScript ecosystem. This cheatsheet covers the commands you run daily: initializing projects, installing and updating dependencies, running scripts, workspaces and monorepos, publishing packages, and auditing for vulnerabilities.
npm commands work for both; where Yarn differs, both forms are shown.
Project Setup
npm init: Interactive project setup: creates package.json.npm init -y: Create package.json with defaults: skip the questions.npm install: Install ALL dependencies listed in package.json (creates node_modules).npm ci: Clean install from package-lock.json: exact versions, deletes node_modules first. Use in CI.yarn init -y: Yarn equivalent of npm init -y.yarn install: Install dependencies with Yarn (also yarn, just running yarn works).yarn set version stable: Switch to Yarn Berry (modern Yarn): enables PnP and workspaces v2.npm pkg set name="myapp" version="1.0.0": Set package.json fields from the CLI.npm pkg delete scripts.test: Remove a package.json field.npm pkg get scripts: Read package.json fields.Installing Packages
npm install <pkg>: Install as a production dependency.npm install -D <pkg>: Install as a dev dependency: tools, linters, test runners.npm install <pkg>@latest: Install the latest version: major upgrades.npm install <pkg>@1.2.3: Install a specific version: pin it.npm install <pkg>@^1.0.0: Install a semver range.npm install -g <pkg>: Install globally: CLI tools like vercel, pm2.npm install --save-exact <pkg>: Pin the exact version in package.json: no ^ or ~.npm uninstall <pkg>: Remove a package.npm uninstall -D <pkg>: Remove a dev dependency.npm ls: List the installed dependency tree: what is actually installed?npm ls --depth=0: Top-level dependencies only: the readable tree.npm ls <pkg>: Why is this package installed? Show its dependents.yarn add <pkg>: Yarn install: same as npm install.yarn add -D <pkg>: Yarn dev dependency.yarn remove <pkg>: Yarn uninstall.npm install <github:user/repo>: Install directly from GitHub: unpublished or forked packages.npm install <pkg> --no-save: Install without touching package.json: try a package first.Scripts & Running
npm run <script>: Run any script from package.json scripts.npm run dev: Common convention: the development server.npm test: Special script: runs without npm run.npm start: Special script: the production entry point.npm run <script> -- --flag: Pass flags through to the underlying command.npm run --silent <script>: Suppress npm's own output: cleaner CI logs.npm run <script> --if-present: Run only if the script exists: safe chaining in CI.npx <cmd>: Run a package WITHOUT installing it: one-off tools.npx <pkg>@latest: Run the latest version of a CLI.npx --yes <pkg>: Skip the install prompt in CI.npm exec <cmd>: The modern form of npx.yarn run <script>: Yarn script runner: bare yarn <script> also works.yarn dlx <cmd>: Yarn's npx equivalent.Workspaces & Monorepos
"workspaces": ["packages/*"]: Declare workspaces in package.json: monorepo setup.npm install (with workspaces): Installs all workspaces: hoists shared deps to the root.npm run <script> -w <workspace>: Run a script in one workspace.npm run <script> --workspaces: Run a script in ALL workspaces.npm install <pkg> -w packages/utils: Install a dependency into a specific workspace.npm ls --workspaces: List all workspace dependencies.yarn workspaces list: List workspaces (Yarn Berry).yarn workspace packages/utils test: Run a script in a specific Yarn workspace.npm pack: Create a tarball of the current package: preview what would be published.npm pack --dry-run: List files that would be packed: check what ships.Publishing & Registry
npm login: Authenticate to the registry.npm whoami: Who is logged in?npm publish: Publish the package to the registry.npm publish --access public: Publish a scoped package publicly.npm publish --tag beta: Publish under a dist-tag: beta releases.npm version patch: Bump the version (1.0.0 → 1.0.1) and create a git tag.npm version minor / major: Bump minor (new features) or major (breaking changes).npm version prerelease --preid=beta: Pre-release version: 1.0.0-beta.1.npm deprecate <pkg>@<version> "message": Warn users a version is deprecated.npm unpublish <pkg>@<version>: Remove a published version: with serious restrictions.npm dist-tag ls <pkg>: List distribution tags.npm dist-tag add <pkg>@1.0.0 latest: Move a tag to a version.npm config set registry https://npm.pkg.github.com/: Point npm at a different registry: GitHub Packages, private mirrors.npm view <pkg>: Show a package's registry info: versions, deps.npm view <pkg> versions: All published versions of a package.npm search <term>: Search the registry.Maintenance & Audits
npm outdated: List packages with newer versions available.npm update: Update packages within your declared semver ranges.npm update <pkg>: Update a single package.npm audit: Check the dependency tree against known vulnerabilities.npm audit fix: Apply safe, non-breaking fixes automatically.npm audit fix --force: Apply breaking-version fixes: last resort, may break things.npm audit --json: Machine-readable audit output: parse in CI.npm dedupe: Flatten duplicate packages: shrink node_modules.npm cache clean --force: Clear the npm cache: fixes corrupt-cache errors.npm cache verify: Verify and clean the cache safely.npm prune: Remove extraneous packages not in package.json.npm prune --production: Remove devDependencies: production deploys.npm explain <pkg>: Why is this package in my tree? Show the dependency chain.npm doctor: Diagnose common npm problems: registry, cache, permissions.npm root -g: Where are global packages installed?npm run env: Show environment variables available to scripts.Frequently asked questions
What is the difference between npm and Yarn?
Both install and manage dependencies. Yarn 1.x added parallel installs and a global cache; Yarn Berry (3+) uses Plug'n'Play and workspaces. npm is bundled with Node and is the default choice for most projects.
What is the difference between dependencies and devDependencies?
dependencies are needed at runtime and installed in production. devDependencies are only for development: build tools, linters, and test frameworks. Use npm install --save-dev to add dev dependencies.
How do I update outdated packages?
Run npm outdated to list outdated packages, then npm update to update within your version ranges. For major version upgrades use npm install <pkg>@latest or tools like npm-check-updates (ncu).
What is npm audit and how do I fix vulnerabilities?
npm audit checks your dependency tree against the advisory database. npm audit fix applies safe, compatible updates automatically, and npm audit fix --force takes breaking updates as a last resort.