$devvkit resources --cheatsheet npm-&-yarn-cheatsheet
npm & Yarn Cheatsheet
[tooling]
Package manager commands for npm and Yarn — install, scripts, workspaces, and publishing.
npm and Yarn are the two dominant JavaScript package managers. This cheatsheet covers the most common commands for both. Yarn commands use the yarn classic syntax (v1), while npm commands target npm v9+.
For newer projects, consider using pnpm for faster, disk-efficient installs.
Project Setup
$
npm init / yarn initInitialize a new package.json interactively.Example: npm init -y # quick setup with defaults
Installing Packages
$
npm install / yarnInstall all dependencies from package.json and lockfile.Example: npm install
$
npm install <pkg> / yarn add <pkg>Install a package and add to dependencies.Example: npm install express
$
npm install -D <pkg> / yarn add -D <pkg>Install as a dev dependency (only for development).Example: npm install -D typescript
$
npm install -g <pkg> / yarn global add <pkg>Install a package globally for CLI use.Example: npm install -g vercel
$
npm uninstall <pkg> / yarn remove <pkg>Remove a package and update package.json.Example: npm uninstall lodash
$
npm ci / yarn install --frozen-lockfileClean install from lockfile only — fails if lockfile is out of sync. For CI.Example: npm ci
Scripts & Running
$
npm run <script> / yarn <script>Run a script defined in package.json scripts.Example: npm run dev
$
npm run --if-present <script>Run a script only if it exists (won't error if missing).Example: npm run --if-present typecheck
$
npx <cmd> / yarn dlx <cmd>Run a CLI tool without installing it globally.Example: npx create-react-app my-app
$
npm exec -- <cmd>Run a command from a local or remote package.Example: npm exec -- prettier --write .
Workspaces & Monorepos
$
npm workspaces / yarn workspacesMonorepo support — run commands across multiple packages.Example: npm run test --workspaces --if-present
Publishing & Registry
$
npm publish / yarn publishPublish the current package to the npm registry.Example: npm publish --access public
$
npm packCreate a tarball of the package as it would be published, for inspection.Example: npm pack
$
npm link / yarn linkSymlink a local package for testing during development.Example: npm link # in the package dir
npm link my-pkg # in the consumer dir
Maintenance & Audits
$
npm ls / yarn listList installed packages and their dependency tree.Example: npm ls --depth=0
$
npm outdated / yarn outdatedShow packages with newer versions available.Example: npm outdated
$
npm update / yarn upgradeUpdate packages to the latest version within the semver range.Example: npm update
$
npm audit / yarn auditScan dependencies for known security vulnerabilities.Example: npm audit fix # auto-fix vulnerabilities
$
npm cache clean --force / yarn cache cleanClear the package manager cache to resolve issues.Example: npm cache clean --force
$
npm dedupeReduce duplication in node_modules by hoisting shared dependencies.Example: npm dedupe
npm & Yarn Cheatsheet
Package manager commands for npm and Yarn — install, scripts, workspaces, and publishing.
npm and Yarn are the two dominant JavaScript package managers. This cheatsheet covers the most common commands for both. Yarn commands use the yarn classic syntax (v1), while npm commands target npm v9+.
For newer projects, consider using pnpm for faster, disk-efficient installs.
Project Setup
npm init / yarn init — Initialize a new package.json interactively.Example: npm init -y # quick setup with defaults
Installing Packages
npm install / yarn — Install all dependencies from package.json and lockfile.Example: npm install
npm install <pkg> / yarn add <pkg> — Install a package and add to dependencies.Example: npm install express
npm install -D <pkg> / yarn add -D <pkg> — Install as a dev dependency (only for development).Example: npm install -D typescript
npm install -g <pkg> / yarn global add <pkg> — Install a package globally for CLI use.Example: npm install -g vercel
npm uninstall <pkg> / yarn remove <pkg> — Remove a package and update package.json.Example: npm uninstall lodash
npm ci / yarn install --frozen-lockfile — Clean install from lockfile only — fails if lockfile is out of sync. For CI.Example: npm ci
Scripts & Running
npm run <script> / yarn <script> — Run a script defined in package.json scripts.Example: npm run dev
npm run --if-present <script> — Run a script only if it exists (won't error if missing).Example: npm run --if-present typecheck
npx <cmd> / yarn dlx <cmd> — Run a CLI tool without installing it globally.Example: npx create-react-app my-app
npm exec -- <cmd> — Run a command from a local or remote package.Example: npm exec -- prettier --write .
Workspaces & Monorepos
npm workspaces / yarn workspaces — Monorepo support — run commands across multiple packages.Example: npm run test --workspaces --if-present
Publishing & Registry
npm publish / yarn publish — Publish the current package to the npm registry.Example: npm publish --access public
npm pack — Create a tarball of the package as it would be published, for inspection.Example: npm pack
npm link / yarn link — Symlink a local package for testing during development.Example: npm link # in the package dir
npm link my-pkg # in the consumer dir
Maintenance & Audits
npm ls / yarn list — List installed packages and their dependency tree.Example: npm ls --depth=0
npm outdated / yarn outdated — Show packages with newer versions available.Example: npm outdated
npm update / yarn upgrade — Update packages to the latest version within the semver range.Example: npm update
npm audit / yarn audit — Scan dependencies for known security vulnerabilities.Example: npm audit fix # auto-fix vulnerabilities
npm cache clean --force / yarn cache clean — Clear the package manager cache to resolve issues.Example: npm cache clean --force
npm dedupe — Reduce duplication in node_modules by hoisting shared dependencies.Example: npm dedupe