Yarn Commands

yarn commands cheatsheet.

Install, add, run and manage workspaces — the yarn commands you use every day. Tap to copy.

Project setup
yarn initCreate a package.json interactively
yarn init -yCreate a package.json with defaults
yarn installInstall all dependencies from package.json
yarnShortcut for yarn install (no arguments)
Dependencies
yarn add <pkg>Add a package as a dependency
yarn add -D <pkg>Add a package as a dev dependency
yarn add <pkg>@<version>Add a specific version of a package
yarn remove <pkg>Remove a package
yarn up <pkg>Upgrade a package to the latest version (classic: yarn upgrade)
Running
yarn run <script>Run a custom script from package.json
yarn <script>Run a script by name directly
yarn dlx <pkg>Run a package binary without installing it (npx equivalent)
yarn nodeRun node with the project's dependencies resolved
Info & maintenance
yarn why <pkg>Explain why a package is installed
yarn outdatedShow packages with newer versions available (classic v1)
yarn info <pkg>Show information about a package
yarn cache cleanClear the yarn cache
Workspaces
yarn workspaces listList all workspaces in the project
yarn workspace <name> <cmd>Run a command in a specific workspace
yarn workspaces foreach <cmd>Run a command across all workspaces

What yarn actually does

Yarn is a package manager for JavaScript projects, an alternative to npm that pulls in code other people wrote and runs your project's tasks. The file at the centre of it all is package.json, which lists your dependencies and defines scripts, alongside a yarn.lock file that pins exact versions so installs are reproducible. When you clone a project, a single yarn install (or just yarn) reads those files and downloads everything the project needs — the first command you'll run on almost any codebase.

Adding the right way

Adding packages has a few flavours worth understanding. yarn add <pkg> adds a normal dependency your app needs to run. yarn add -D <pkg> marks it as a dev dependency — tools like test runners and bundlers that you need while building but not in production. You can pin a version with yarn add <pkg>@<version>, and remove things with yarn remove <pkg>. Getting these distinctions right keeps your project lean and your deployments clean.

Scripts and dlx

The scripts section of package.json is where projects define their commands, run with yarn run <name> — or, more often, just yarn <name>, since Yarn lets you call a script by name directly. The companion command yarn dlx is Yarn's answer to npx: it runs a package's executable without permanently installing it, which is perfect for one-off scaffolding tools like yarn dlx create-react-app or for trying something once.

Classic, Berry and workspaces

Yarn comes in two eras: classic (v1) and modern Berry (v2+), and a few commands differ between them — for example the modern yarn up replaces classic's yarn upgrade. For monorepos, workspaces let one repository hold many packages: yarn workspace <name> <cmd> targets a single one, while yarn workspaces foreach fans a command out across them all. These pair naturally with the npm commands when you switch package managers and the Git commands for version control.

FAQ

What's the difference between yarn add and yarn add -D?
yarn add <pkg> installs the package as a normal dependency your app needs to run; yarn add -D <pkg> (short for --dev) adds it as a dev dependency — tooling like test runners and bundlers you need while building but not in production.
What does yarn dlx do?
yarn dlx runs a package's executable without permanently installing it — the modern Yarn equivalent of npx, perfect for one-off tools like yarn dlx create-react-app.
Does yarn install differ from just running yarn?
No — running yarn with no arguments is a shortcut for yarn install. Both read package.json and the lockfile and install every dependency the project needs.

More cheatsheets