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 📊

Common YARN Commands 🖥️
Installation:
npm install -g yarn
Initialize a new project:
yarn init
Add a dependency:
yarn add [package-name]
yarn add [package-name]@[version]
yarn add [package-name] --dev
Remove a dependency:
yarn remove [package-name]
Install all dependencies:
yarn install
Run a script:
yarn run [script-name]
Upgrade packages:
yarn upgrade
yarn upgrade [package-name]
yarn upgrade [package-name]@[version]
Code Snippets 💻
Example package.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):
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:
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:
yarn build
Run tests before deployment:
yarn test
Start in production mode:
yarn start:prod
Remember to add these scripts to your package.json file:
{
"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.
Last updated
Was this helpful?