# Yarn — Package and Project Manager for Node.js

> Practical guide to Yarn — fast JavaScript package manager and npm alternative with workspaces, offline caching and deterministic installs via lockfile.

Source: https://www.jpkc.com/db/en/cheatsheets/package-managers/yarn/

<!-- PROSE:intro -->
Yarn is a fast JavaScript package manager and a well-established alternative to npm. Through its `yarn.lock` file, Yarn keeps your dependencies reproducible and installs them quickly thanks to an offline cache. With workspaces you can manage monorepos made up of several packages in a single project. It is worth knowing the difference between Yarn Classic (v1) and modern "Berry" (v2+), which introduces a different installation model via Plug'n'Play – so some commands differ between the two lines.
<!-- PROSE:intro:end -->

## Install & Init

`yarn init` — Create a new package.json interactively.

```bash
yarn init
```

`yarn init -y` — Create package.json with default values.

```bash
yarn init -y
```

`yarn install` — Install all dependencies from package.json.

```bash
yarn install
```

`yarn` — Shorthand for yarn install.

```bash
yarn
```

`yarn install --frozen-lockfile` — Install without updating yarn.lock (CI-safe).

```bash
yarn install --frozen-lockfile
```

## Add & Remove Packages

`yarn add <package>` — Add a package as a dependency.

```bash
yarn add express
```

`yarn add <package>@<version>` — Add a specific version.

```bash
yarn add react@18.2.0
```

`yarn add -D <package>` — Add as a dev dependency.

```bash
yarn add -D typescript eslint
```

`yarn add --peer <package>` — Add as a peer dependency.

```bash
yarn add --peer react
```

`yarn remove <package>` — Remove a package.

```bash
yarn remove lodash
```

`yarn add <package> --exact` — Add with exact version (no ^ or ~ prefix).

```bash
yarn add react --exact
```

## Upgrade & Info

`yarn upgrade <package>` — Upgrade a package to the latest within range.

```bash
yarn upgrade express
```

`yarn upgrade --latest` — Upgrade all packages to their latest versions.

```bash
yarn upgrade --latest
```

`yarn outdated` — Show outdated packages.

```bash
yarn outdated
```

`yarn info <package>` — Show detailed information about a package.

```bash
yarn info react
```

`yarn list` — List installed packages.

```bash
yarn list --depth=0
```

`yarn why <package>` — Show why a package is installed (dependency chain).

```bash
yarn why webpack
```

## Scripts & Run

`yarn run <script>` — Run a script defined in package.json.

```bash
yarn run build
```

`yarn <script>` — Shorthand for yarn run.

```bash
yarn build
```

`yarn start` — Run the start script.

```bash
yarn start
```

`yarn test` — Run the test script.

```bash
yarn test
```

`yarn dlx <package>` — Execute a package without installing (like npx).

```bash
yarn dlx create-react-app my-app
```

## Workspaces

`yarn workspaces list` — List all workspaces in the project.

```bash
yarn workspaces list
```

`yarn workspace <name> add <package>` — Add a package to a specific workspace.

```bash
yarn workspace @myapp/server add express
```

`yarn workspace <name> run <script>` — Run a script in a specific workspace.

```bash
yarn workspace @myapp/client run build
```

`yarn workspaces foreach run <script>` — Run a script in all workspaces.

```bash
yarn workspaces foreach run test
```

## Cache & Config

`yarn cache clean` — Clear the global package cache.

```bash
yarn cache clean
```

`yarn config set <key> <value>` — Set a configuration value.

```bash
yarn config set registry https://registry.npmjs.org
```

`yarn config list` — Show all configuration values.

```bash
yarn config list
```

`yarn global add <package>` — Install a package globally (Yarn Classic).

```bash
yarn global add serve
```

<!-- PROSE:outro -->
## Conclusion

Yarn combines fast, reproducible installs, an offline cache and first-class workspace support in a single tool – ideal for monorepos and larger projects. Pin versions deliberately (`--exact`) and use `yarn install --frozen-lockfile` (Classic) or `yarn install --immutable` (Berry) in CI pipelines so builds stay reproducible and the `yarn.lock` is not changed unnoticed. Keep an eye on your dependencies' install scripts – they are a common supply-chain vector. And remember not to mix Classic and Berry commands: what works under v1 is often named differently under v2+.

## Further Reading

- [yarnpkg.com](https://yarnpkg.com/) – official project site with documentation and CLI reference
- [Yarn – GitHub project](https://github.com/yarnpkg/berry) – source code, releases and issues
<!-- PROSE:outro:end -->

## Related Commands

- [apk](https://www.jpkc.com/db/en/cheatsheets/package-managers/apk/) – package manager for Alpine Linux
- [apt](https://www.jpkc.com/db/en/cheatsheets/package-managers/apt/) – package management for Debian and Ubuntu
- [apt-get](https://www.jpkc.com/db/en/cheatsheets/package-managers/apt-get/) – classic package tool for Debian and Ubuntu

