How to install packages with yarn in Node.js

Installing packages with yarn in Node.js provides faster, more reliable, and deterministic dependency management compared to npm, with better offline capabilities and security features. As the creator of CoreUI, a widely used open-source UI library, I’ve used yarn extensively across Node.js projects for building component libraries, managing dependencies in enterprise applications, and maintaining consistent package installations across development teams. From my expertise, the most effective approach is to use yarn’s add command with proper dependency categorization. This method provides deterministic installations, faster package resolution, and excellent offline capabilities while maintaining compatibility with npm registry and package ecosystem.

Use yarn add command to install packages with fast, reliable, and deterministic dependency management in Node.js.

# Install Yarn first (if not already installed)
# Via npm
npm install -g yarn

# Via Homebrew (macOS)
brew install yarn

# Via Curl
curl -o- -L https://yarnpkg.com/install.sh | bash

# Via package manager (Ubuntu/Debian)
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update && sudo apt install yarn

# Verify installation
yarn --version

# 1. Basic package installation
yarn add express                    # Production dependency
yarn add lodash                     # Single package
yarn add react react-dom           # Multiple packages

# 2. Development dependencies
yarn add --dev eslint              # Development only
yarn add -D prettier               # Short form
yarn add --dev @types/node jest    # Multiple dev dependencies

# 3. Peer dependencies
yarn add --peer react              # Peer dependency

# 4. Optional dependencies
yarn add --optional sharp          # Optional dependency

# 5. Global packages
yarn global add nodemon            # Global installation
yarn global add @vue/cli           # Global CLI tools

# 6. Specific version installation
yarn add [email protected]           # Exact version
yarn add lodash@^4.17.0           # Compatible version
yarn add react@~18.2.0            # Approximate version
yarn add typescript@latest        # Latest version
yarn add react@beta               # Beta version

# 7. Install from different sources
yarn add https://github.com/user/repo.git        # Git repository
yarn add git+ssh://[email protected]:user/repo.git  # SSH Git
yarn add file:../local-package                   # Local package
yarn add user/repo                               # GitHub shorthand

# 8. Install all dependencies from package.json
yarn install                       # Install all dependencies
yarn                              # Shorthand for yarn install
yarn install --frozen-lockfile   # Use exact versions from lockfile
yarn install --production        # Install only production dependencies

# 9. Workspace management (monorepos)
yarn add express --workspace=api
yarn add react --workspace=frontend
yarn workspaces foreach install

# 10. Advanced installation options
yarn add express --exact          # Install exact version
yarn add lodash --tilde           # Use tilde range
yarn add react --caret            # Use caret range (default)
yarn add webpack --dev --exact    # Exact dev dependency

# Example: Setting up a new Node.js project with common packages
mkdir my-node-project
cd my-node-project

# Initialize with yarn
yarn init -y

# Install production dependencies
yarn add express
yarn add cors
yarn add helmet
yarn add dotenv
yarn add mongoose

# Install development dependencies
yarn add --dev nodemon
yarn add --dev eslint
yarn add --dev prettier
yarn add --dev jest
yarn add --dev supertest

# Install TypeScript stack
yarn add --dev typescript
yarn add --dev @types/node
yarn add --dev @types/express
yarn add --dev ts-node

# View installed packages
yarn list                         # All packages
yarn list --depth=0              # Top-level only
yarn list --pattern="express"    # Specific pattern

# Check package information
yarn info express                # Package information
yarn info express versions       # Available versions
yarn why express                 # Why package is installed

# Update packages
yarn upgrade                      # Update all packages
yarn upgrade express             # Update specific package
yarn upgrade --latest           # Update to latest versions

# Remove packages
yarn remove express              # Remove package
yarn remove eslint --dev        # Remove dev dependency
yarn global remove nodemon      # Remove global package
{
  "name": "my-node-project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js",
    "test": "jest",
    "lint": "eslint .",
    "format": "prettier --write ."
  },
  "dependencies": {
    "express": "^4.18.2",
    "cors": "^2.8.5",
    "helmet": "^6.1.5",
    "dotenv": "^16.0.3",
    "mongoose": "^7.0.3"
  },
  "devDependencies": {
    "nodemon": "^2.0.22",
    "eslint": "^8.39.0",
    "prettier": "^2.8.8",
    "jest": "^29.5.0",
    "supertest": "^6.3.3",
    "typescript": "^5.0.4",
    "@types/node": "^18.16.3",
    "@types/express": "^4.17.17",
    "ts-node": "^10.9.1"
  }
}
# Working with yarn.lock file
# The yarn.lock file ensures deterministic installations
# Never edit yarn.lock manually
# Always commit yarn.lock to version control

# Check for outdated packages
yarn outdated

# Audit for security vulnerabilities
yarn audit
yarn audit --level moderate      # Only moderate and higher
yarn audit fix                   # Attempt to fix vulnerabilities

# Cache management
yarn cache list                  # Show cached packages
yarn cache dir                   # Show cache directory
yarn cache clean                 # Clean cache
yarn cache clean express         # Clean specific package

# Configuration
yarn config list                 # Show configuration
yarn config set registry https://registry.npmjs.org/
yarn config set init-license MIT
yarn config set init-version 0.1.0

# Performance and debugging
yarn install --verbose          # Verbose output
yarn install --silent          # Silent installation
yarn install --no-progress     # No progress bar
yarn install --network-timeout 1000000  # Extended timeout

# Offline mode
yarn install --offline         # Use only cached packages

# Workspace commands (for monorepos)
yarn workspaces info           # Show workspace information
yarn workspace api add express # Add to specific workspace
yarn workspaces foreach run build  # Run command in all workspaces

# Plugin system (Yarn 2+)
yarn plugin import @yarnpkg/plugin-version
yarn plugin list

# Create .yarnrc.yml for project configuration
cat > .yarnrc.yml << 'EOF'
nodeLinker: node-modules
yarnPath: .yarn/releases/yarn-3.5.1.cjs
enableGlobalCache: false
compressionLevel: mixed
EOF
# Troubleshooting common issues

# Clear cache and reinstall
yarn cache clean
rm -rf node_modules
rm yarn.lock
yarn install

# Fix permission issues
sudo chown -R $(whoami) ~/.yarn

# Network issues
yarn install --network-timeout 1000000
yarn config set network-timeout 1000000

# Registry issues
yarn config set registry https://registry.npmjs.org/
yarn config set registry https://registry.yarnpkg.com/

# Check Yarn version and update
yarn --version
yarn set version latest         # Yarn 2+
yarn policies set-version latest  # Yarn 1.x

# Verify installation integrity
yarn install --check-files
yarn check --integrity

# Environment-specific installations
NODE_ENV=production yarn install --production
NODE_ENV=development yarn install

Yarn provides faster and more reliable package installations than npm through parallel downloads, cached packages, and deterministic lockfiles. The yarn add command automatically updates both package.json and yarn.lock files. Use --dev flag for development dependencies and --exact for specific versions. Yarn’s offline mode allows installations without internet connectivity using cached packages. The yarn.lock file ensures identical dependency trees across different environments and should always be committed to version control. Yarn workspaces provide excellent monorepo support for managing multiple packages.

Best Practice Note:

This is the same yarn usage approach we implement in CoreUI Node.js projects and enterprise development workflows. Always commit yarn.lock to version control, use exact versions for critical dependencies, organize dependencies by type (production, development, peer), regularly audit for security vulnerabilities, and use workspaces for monorepo management when building multiple related packages.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
How to sort an array of objects by string property value in JavaScript
How to sort an array of objects by string property value in JavaScript

How to convert a string to boolean in JavaScript
How to convert a string to boolean in JavaScript

How to set focus on an input field after rendering in React
How to set focus on an input field after rendering in React

How to Hide Scrollbar with CSS
How to Hide Scrollbar with CSS

Answers by CoreUI Core Team