yarn commands cheatsheet.
Install, add, run and manage workspaces — the yarn commands you use every day. Tap to copy.
yarn initCreate a package.json interactivelyyarn init -yCreate a package.json with defaultsyarn installInstall all dependencies from package.jsonyarnShortcut for yarn install (no arguments)yarn add <pkg>Add a package as a dependencyyarn add -D <pkg>Add a package as a dev dependencyyarn add <pkg>@<version>Add a specific version of a packageyarn remove <pkg>Remove a packageyarn up <pkg>Upgrade a package to the latest version (classic: yarn upgrade)yarn run <script>Run a custom script from package.jsonyarn <script>Run a script by name directlyyarn dlx <pkg>Run a package binary without installing it (npx equivalent)yarn nodeRun node with the project's dependencies resolvedyarn why <pkg>Explain why a package is installedyarn outdatedShow packages with newer versions available (classic v1)yarn info <pkg>Show information about a packageyarn cache cleanClear the yarn cacheyarn workspaces listList all workspaces in the projectyarn workspace <name> <cmd>Run a command in a specific workspaceyarn workspaces foreach <cmd>Run a command across all workspacesWhat 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.