Peer Dependencies Resolution - Complete Guide
Published: September 25, 2024 | Reading time: 19 minutes
Peer Dependencies Overview
Peer dependencies ensure compatibility between packages:
Peer Dependency Benefits
# Peer Dependency Benefits
- Package compatibility
- Version consistency
- Dependency sharing
- Conflict prevention
- Plugin architecture
- Framework integration
- Library compatibility
Understanding Peer Dependencies
Peer Dependency Concepts
Peer Dependency Concepts
# Peer Dependency Concepts
# 1. Basic Peer Dependency
# package.json
{
"name": "react-component",
"peerDependencies": {
"react": ">=16.8.0",
"react-dom": ">=16.8.0"
}
}
# 2. Version Range Peer Dependencies
{
"peerDependencies": {
"react": ">=16.8.0 <19.0.0",
"typescript": ">=4.0.0 <5.0.0"
}
}
# 3. Optional Peer Dependencies
{
"peerDependencies": {
"react": ">=16.8.0",
"vue": ">=2.6.0"
},
"peerDependenciesMeta": {
"vue": {
"optional": true
}
}
}
# 4. Multiple Version Support
{
"peerDependencies": {
"react": ">=16.8.0 || >=17.0.0 || >=18.0.0"
}
}
# 5. Peer Dependency with DevDependencies
{
"peerDependencies": {
"react": ">=16.8.0"
},
"devDependencies": {
"react": "^18.0.0",
"@types/react": "^18.0.0"
}
}
# 6. Scoped Peer Dependencies
{
"peerDependencies": {
"@angular/core": ">=12.0.0",
"@angular/common": ">=12.0.0"
}
}
# 7. Peer Dependency with Bundler
{
"peerDependencies": {
"webpack": ">=4.0.0",
"rollup": ">=2.0.0"
}
}
# 8. Framework Peer Dependencies
{
"peerDependencies": {
"next": ">=12.0.0",
"react": ">=17.0.0"
}
}
# 9. Testing Framework Peer Dependencies
{
"peerDependencies": {
"jest": ">=26.0.0",
"@testing-library/react": ">=12.0.0"
}
}
# 10. Build Tool Peer Dependencies
{
"peerDependencies": {
"typescript": ">=4.0.0",
"eslint": ">=7.0.0"
}
}
NPM Peer Dependency Resolution
NPM Resolution Strategies
NPM Peer Dependency Resolution
# NPM Peer Dependency Resolution
# 1. Install with Peer Dependencies
npm install package-name
npm install package-name --save-peer
# 2. Install Peer Dependencies Manually
npm install react@^18.0.0
npm install react-dom@^18.0.0
# 3. Install with Legacy Peer Deps
npm install --legacy-peer-deps
npm install package-name --legacy-peer-deps
# 4. Install with Force
npm install --force
npm install package-name --force
# 5. Install with Strict Peer Deps
npm install --strict-peer-deps
npm install package-name --strict-peer-deps
# 6. Install with Auto Install Peer Deps
npm install --auto-install-peers
npm install package-name --auto-install-peers
# 7. Install with Peer Deps Meta
npm install --install-peer-deps
npm install package-name --install-peer-deps
# 8. Install with Override
npm install --override package-name@version
npm install package-name --override
# 9. Install with Resolution
npm install --resolution package-name@version
npm install package-name --resolution
# 10. Install with Package Lock Only
npm install --package-lock-only
npm install package-name --package-lock-only
# 11. Install with No Package Lock
npm install --no-package-lock
npm install package-name --no-package-lock
# 12. Install with No Audit
npm install --no-audit
npm install package-name --no-audit
# 13. Install with No Fund
npm install --no-fund
npm install package-name --no-fund
# 14. Install with No Optional
npm install --no-optional
npm install package-name --no-optional
# 15. Install with No Save
npm install --no-save
npm install package-name --no-save
Yarn Peer Dependency Resolution
Yarn Resolution Methods
Yarn Peer Dependency Resolution
# Yarn Peer Dependency Resolution
# 1. Install with Peer Dependencies
yarn add package-name
yarn add package-name --peer
# 2. Install Peer Dependencies Manually
yarn add react@^18.0.0
yarn add react-dom@^18.0.0
# 3. Install with Legacy Peer Deps
yarn install --legacy-peer-deps
yarn add package-name --legacy-peer-deps
# 4. Install with Force
yarn install --force
yarn add package-name --force
# 5. Install with Strict Peer Deps
yarn install --strict-peer-deps
yarn add package-name --strict-peer-deps
# 6. Install with Auto Install Peer Deps
yarn install --auto-install-peers
yarn add package-name --auto-install-peers
# 7. Install with Peer Deps Meta
yarn install --install-peer-deps
yarn add package-name --install-peer-deps
# 8. Install with Override
yarn add package-name@version --override
yarn add package-name --override
# 9. Install with Resolution
yarn add package-name@version --resolution
yarn add package-name --resolution
# 10. Install with Frozen Lockfile
yarn install --frozen-lockfile
yarn add package-name --frozen-lockfile
# 11. Install with No Lockfile
yarn install --no-lockfile
yarn add package-name --no-lockfile
# 12. Install with No Audit
yarn install --no-audit
yarn add package-name --no-audit
# 13. Install with No Fund
yarn install --no-fund
yarn add package-name --no-fund
# 14. Install with No Optional
yarn install --no-optional
yarn add package-name --no-optional
# 15. Install with No Save
yarn add package-name --no-save
Resolution Strategies
Conflict Resolution Methods
Resolution Strategies
- Version alignment
- Dependency updates
- Package replacement
- Configuration overrides
- Manual resolution
- Legacy mode
- Force installation
Best Practices
- Version compatibility
- Dependency management
- Testing strategies
- Documentation
- Team coordination
- CI/CD validation
- Monitoring
Advanced Resolution Techniques
Complex Resolution Scenarios
Advanced Resolution
# Advanced Resolution Techniques
# 1. Package Resolution Override
# package.json
{
"overrides": {
"react": "^18.0.0",
"react-dom": "^18.0.0"
}
}
# 2. Yarn Resolution
# package.json
{
"resolutions": {
"react": "^18.0.0",
"react-dom": "^18.0.0"
}
}
# 3. NPM Resolution
# package.json
{
"overrides": {
"react": "^18.0.0",
"react-dom": "^18.0.0"
}
}
# 4. Workspace Resolution
# package.json
{
"workspaces": [
"packages/*"
],
"overrides": {
"react": "^18.0.0"
}
}
# 5. Scoped Resolution
# package.json
{
"overrides": {
"@company/package": "^2.0.0"
}
}
# 6. Conditional Resolution
# package.json
{
"overrides": {
"react": "^18.0.0",
"react-dom": "^18.0.0"
},
"engines": {
"node": ">=16.0.0"
}
}
# 7. Environment Resolution
# package.json
{
"overrides": {
"react": "^18.0.0"
},
"scripts": {
"postinstall": "npm run resolve-peer-deps"
}
}
# 8. Custom Resolution Script
# resolve-peer-deps.js
const { execSync } = require('child_process');
try {
execSync('npm install react@^18.0.0', { stdio: 'inherit' });
execSync('npm install react-dom@^18.0.0', { stdio: 'inherit' });
} catch (error) {
console.error('Peer dependency resolution failed');
process.exit(1);
}
# 9. CI/CD Resolution
# .github/workflows/peer-deps.yml
name: Peer Dependencies
on: [push, pull_request]
jobs:
peer-deps:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
- run: npm install --legacy-peer-deps
- run: npm run build
# 10. Docker Resolution
# Dockerfile
FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install --legacy-peer-deps
COPY . .
RUN npm run build
Summary
Peer dependencies resolution involves several key components:
- Understanding: Peer dependency concepts and version ranges
- NPM Resolution: Legacy, force, and strict peer dependency handling
- Yarn Resolution: Yarn-specific peer dependency resolution methods
- Advanced Techniques: Overrides, resolutions, and custom scripts
Need More Help?
Struggling with peer dependencies resolution or need help implementing resolution strategies? Our package management experts can help you resolve dependency conflicts.
Get Peer Dependencies Help