Monorepo Setup Tools - Complete Guide
Published: September 25, 2024 | Reading time: 21 minutes
Monorepo Overview
Monorepo tools manage multiple packages in a single repository:
Monorepo Benefits
# Monorepo Benefits
- Code sharing
- Unified tooling
- Atomic changes
- Dependency management
- Build optimization
- Team collaboration
- Version synchronization
NPM Workspaces
NPM Workspace Configuration
NPM Workspaces
# NPM Workspaces
# 1. Root package.json
{
"name": "my-monorepo",
"version": "1.0.0",
"workspaces": [
"packages/*",
"apps/*"
],
"scripts": {
"build": "npm run build --workspaces",
"test": "npm run test --workspaces",
"lint": "npm run lint --workspaces"
}
}
# 2. Workspace Structure
my-monorepo/
├── package.json
├── packages/
│ ├── shared/
│ │ └── package.json
│ └── utils/
│ └── package.json
└── apps/
├── web/
│ └── package.json
└── mobile/
└── package.json
# 3. Install Dependencies
npm install
# 4. Add Dependency to Workspace
npm install react --workspace=packages/shared
npm install lodash --workspace=apps/web
# 5. Run Scripts in Workspaces
npm run build --workspace=packages/shared
npm run test --workspaces
# 6. Install from Workspace
npm install @my-org/shared --workspace=apps/web
# 7. Publish Workspace
npm publish --workspace=packages/shared
# 8. Workspace Commands
npm run build --workspaces --if-present
npm run test --workspaces --if-present
# 9. Workspace Configuration
# .npmrc
workspace-concurrency=4
prefer-workspace-packages=true
# 10. Workspace Scripts
npm run build --workspaces --if-present
npm run test --workspaces --if-present
npm run lint --workspaces --if-present
Yarn Workspaces
Yarn Workspace Management
Yarn Workspaces
# Yarn Workspaces
# 1. Root package.json
{
"name": "my-monorepo",
"version": "1.0.0",
"private": true,
"workspaces": [
"packages/*",
"apps/*"
],
"scripts": {
"build": "yarn workspaces run build",
"test": "yarn workspaces run test",
"lint": "yarn workspaces run lint"
}
}
# 2. Install Dependencies
yarn install
# 3. Add Dependency to Workspace
yarn workspace shared add react
yarn workspace web add lodash
# 4. Run Scripts in Workspaces
yarn workspace shared build
yarn workspaces run test
# 5. Install from Workspace
yarn workspace web add @my-org/shared
# 6. Publish Workspace
yarn workspace shared publish
# 7. Workspace Information
yarn workspaces info
yarn workspaces list
# 8. Workspace Commands
yarn workspaces run build --if-present
yarn workspaces run test --if-present
# 9. Workspace Configuration
# .yarnrc.yml
workspaceConcurrency: 4
preferWorkspacePackages: true
# 10. Workspace Scripts
yarn workspaces run build --if-present
yarn workspaces run test --if-present
yarn workspaces run lint --if-present
Lerna
Lerna Monorepo Management
Lerna Configuration
# Lerna Configuration
# 1. Install Lerna
npm install -g lerna
npm install --save-dev lerna
# 2. Initialize Lerna
lerna init
# 3. Lerna Configuration
# lerna.json
{
"version": "independent",
"npmClient": "npm",
"command": {
"publish": {
"conventionalCommits": true,
"message": "chore(release): publish"
},
"bootstrap": {
"ignore": "npm-*",
"npmClientArgs": ["--no-package-lock"]
}
},
"packages": ["packages/*", "apps/*"]
}
# 4. Bootstrap Packages
lerna bootstrap
# 5. Run Scripts
lerna run build
lerna run test
lerna run lint
# 6. Add Dependencies
lerna add react --scope=shared
lerna add lodash --scope=web
# 7. Publish Packages
lerna publish
# 8. Version Management
lerna version
lerna version --conventional-commits
# 9. Clean Packages
lerna clean
# 10. List Packages
lerna list
lerna list --long
# 11. Execute Commands
lerna exec -- npm run build
lerna exec -- rm -rf node_modules
# 12. Link Packages
lerna link
lerna link convert
Nx
Nx Monorepo Platform
Nx Configuration
# Nx Configuration
# 1. Install Nx
npm install -g nx
npx create-nx-workspace@latest my-workspace
# 2. Nx Configuration
# nx.json
{
"version": 2,
"projects": {
"shared": "packages/shared",
"web": "apps/web",
"mobile": "apps/mobile"
},
"targetDefaults": {
"build": {
"dependsOn": ["^build"],
"inputs": ["production", "^production"]
},
"test": {
"inputs": ["default", "^production", "{workspaceRoot}/jest.preset.js"]
}
}
}
# 3. Generate Applications
nx generate @nx/react:application web
nx generate @nx/react:library shared
# 4. Run Tasks
nx build web
nx test shared
nx lint web
# 5. Run Multiple Tasks
nx run-many --target=build --all
nx run-many --target=test --all
# 6. Graph Visualization
nx graph
nx graph --file=graph.json
# 7. Affected Commands
nx affected --target=build
nx affected --target=test
# 8. Cache Management
nx reset
nx show projects
# 9. Workspace Commands
nx run-many --target=build --all --parallel=3
nx run-many --target=test --all --parallel=3
# 10. Plugin Management
nx add @nx/react
nx add @nx/next
nx add @nx/node
Rush
Rush Monorepo Manager
Rush Configuration
# Rush Configuration
# 1. Install Rush
npm install -g @microsoft/rush
rush init
# 2. Rush Configuration
# rush.json
{
"rushVersion": "5.82.0",
"npmVersion": "8.19.2",
"nodeSupportedVersionRange": ">=16.13.0 <19.0.0",
"projectFolderMaxDepth": 3,
"projects": [
{
"packageName": "shared",
"projectFolder": "packages/shared"
},
{
"packageName": "web",
"projectFolder": "apps/web"
}
]
}
# 3. Install Dependencies
rush install
# 4. Build Projects
rush build
rush build --to shared
# 5. Test Projects
rush test
rush test --to shared
# 6. Add Dependencies
rush add --package react --to shared
rush add --package lodash --to web
# 7. Publish Projects
rush publish
rush publish --include-all
# 8. Update Dependencies
rush update
rush update --full
# 9. Clean Projects
rush purge
rush purge --unsafe
# 10. List Projects
rush list
rush list --json
# 11. Change Management
rush change
rush change --bulk
# 12. Version Management
rush version
rush version --bump
Monorepo Best Practices
Management Strategies
Best Practices
- Consistent tooling
- Shared configurations
- Dependency management
- Build optimization
- Testing strategies
- Version management
- Documentation
Common Challenges
- Build performance
- Dependency conflicts
- Version synchronization
- Team coordination
- CI/CD complexity
- Tool integration
- Scalability issues
Summary
Monorepo setup tools involve several key components:
- NPM Workspaces: Built-in workspace management
- Yarn Workspaces: Yarn-specific workspace features
- Lerna: Traditional monorepo management
- Nx: Modern monorepo platform
- Rush: Enterprise monorepo solution
Need More Help?
Struggling with monorepo setup or need help choosing the right monorepo tool? Our monorepo experts can help you implement effective workspace management.
Get Monorepo Help