> For the complete documentation index, see [llms.txt](https://www.ankitavirani.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.ankitavirani.com/experience/packege-mangers/yarn-2-berry.md).

# Yarn 2 (Berry)

## YARN 2 (Berry) - The Modern Package Manager 🧶

Yarn 2, also known as Berry, is a significant evolution of the original Yarn package manager. It brings improved performance, enhanced security, and new features to JavaScript and Node.js development.

### Architecture and Concepts 🏗️

Yarn 2's architecture is designed for efficiency, reliability, and improved developer experience:

* 📁 **Plug'n'Play (PnP):** A new resolution strategy that replaces node\_modules
* 🔒 **Zero-Installs:** Allows committing dependencies to version control
* 🌐 **Workspaces:** Improved monorepo support
* 🔧 **Constraints:** Enforce rules across your project
* 📦 **Protocols:** Flexible ways to fetch packages

#### Key Concepts:

* 🚀 **Performance:** Faster installs and reduced disk usage
* 🔐 **Security:** Improved package resolution and validation
* 🛠️ **Extensibility:** Plugin system for custom functionality
* 📜 **TypeScript Support:** Better integration with TypeScript projects

### Yarn 2 Architecture Diagram 📊

<figure><img src="/files/lBeleVj2Orq1u1sLxGIq" alt=""><figcaption></figcaption></figure>

### Common Yarn 2 Commands 🖥️

#### Installation:

```bash
npm install -g yarn
yarn set version berry
```

#### Initialize a new project:

```bash
yarn init -2
```

#### Add a dependency:

```bash
yarn add [package-name]
yarn add [package-name]@[version]
yarn add -D [package-name]  # Add as dev dependency
```

#### Remove a dependency:

```bash
yarn remove [package-name]
```

#### Install all dependencies:

```bash
yarn install
```

#### Run a script:

```bash
yarn [script-name]
```

#### Update packages:

```bash
yarn up
yarn up [package-name]
```

### Code Snippets 💻

#### Example package.json:

```json
{
  "name": "my-yarn2-project",
  "version": "1.0.0",
  "dependencies": {
    "express": "^4.17.1"
  },
  "devDependencies": {
    "typescript": "^4.5.4"
  },
  "scripts": {
    "start": "ts-node src/index.ts",
    "build": "tsc",
    "test": "jest"
  }
}
```

#### Example TypeScript file (src/index.ts):

```tsx
import express from 'express';

const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.send('Hello World from Yarn 2 project!');
});

app.listen(port, () => {
  console.log(`Server running at <http://localhost>:${port}`);
});
```

#### Running the application:

```bash
yarn start
```

Output:

```jsx
Server running at <http://localhost:3000>
```

### Deployment Commands 🚀

Here are some common deployment commands:

#### Build for production:

```bash
yarn build
```

#### Run tests before deployment:

```bash
yarn test
```

#### Start in production mode:

```bash
yarn start:prod
```

Add these scripts to your package.json:

```json
{
  "scripts": {
    "build": "tsc",
    "test": "jest",
    "start:prod": "NODE_ENV=production node dist/index.js"
  }
}
```

### Conclusion 🎉

Yarn 2 (Berry) is a powerful and efficient package manager for Node.js projects. Its innovative features like Plug'n'Play and Zero-Installs provide significant improvements in performance and project management. By leveraging Yarn 2's capabilities, developers can create more efficient, secure, and maintainable JavaScript and TypeScript applications.
