> 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.md).

# Yarn

## YARN 🧶

YARN is a fast, reliable, and secure dependency management tool for Node.js. It was developed by Facebook in 2016 as an alternative to npm (Node Package Manager).

### Architecture and Concepts 🏗️

YARN's architecture is designed for efficiency and reliability:

* 📁 **Workspace:** The root directory of your project
* 📄 **package.json:** Defines project dependencies and scripts
* 🔒 **yarn.lock:** Ensures consistent installs across machines
* 📦 **node\_modules:** Directory where packages are installed
* 🌐 **Registry:** Default is npm registry, but can be changed

#### Key Concepts:

* 🔄 **Offline Mode:** Install packages without internet connection
* 🚀 **Parallel Installation:** Faster package installations
* 🔍 **Flat Mode:** Resolves version conflicts efficiently
* 🔐 **Checksums:** Verifies integrity of installed packages

### YARN Architecture Diagram 📊

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

### Common YARN Commands 🖥️

#### Installation:

```bash
npm install -g yarn
```

#### Initialize a new project:

```bash
yarn init
```

#### Add a dependency:

```bash
yarn add [package-name]
yarn add [package-name]@[version]
yarn add [package-name] --dev
```

#### Remove a dependency:

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

#### Install all dependencies:

```bash
yarn install
```

#### Run a script:

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

#### Upgrade packages:

```bash
yarn upgrade
yarn upgrade [package-name]
yarn upgrade [package-name]@[version]
```

### Code Snippets 💻

#### Example package.json:

```json
{
  "name": "my-project",
  "version": "1.0.0",
  "dependencies": {
    "express": "^4.17.1"
  },
  "devDependencies": {
    "jest": "^27.0.6"
  },
  "scripts": {
    "start": "node index.js",
    "test": "jest"
  }
}
```

#### Example JavaScript file (index.js):

```jsx
const express = require('express');
const app = express();
const port = 3000;

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

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

#### Running the application:

```bash
yarn start
```

Output:

```
Example app listening at <http://localhost:3000>
```

### Deployment Commands 🚀

Deployment commands may vary depending on your hosting platform. Here are some general examples:

#### Build for production:

```bash
yarn build
```

#### Run tests before deployment:

```bash
yarn test
```

#### Start in production mode:

```bash
yarn start:prod
```

Remember to add these scripts to your package.json file:

```json
{
  "scripts": {
    "build": "your-build-command",
    "test": "jest",
    "start:prod": "NODE_ENV=production node index.js"
  }
}
```

### Conclusion 🎉

YARN is a powerful and efficient package manager for Node.js projects. Its architecture and features make it a popular choice for many developers. By understanding its concepts and commands, you can streamline your development process and manage dependencies more effectively.
