Add Dependencies
In a monorepo, you may have multiple packages that share common dependencies. To avoid installing the same dependencies multiple times, you can use the monopkg add
command to install dependencies for all packages in the monorepo at once. This not only saves time but also ensures consistency across your packages.
By using the monopkg add
command, you can specify various options to tailor the installation process to your needs. For instance, you can include or exclude specific packages, install dependencies as dev or peer dependencies, and even target specific workspaces within your monorepo. This flexibility allows you to manage your dependencies efficiently and keep your project organized.
Usage
This command allows you to install dependencies for all packages in the monorepo at once.
monopkg add [options] <dependencies...>
bun x monopkg add [options] <dependencies...>
npx monopkg add [options] <dependencies...>
yarn dlx monopkg add [options] <dependencies...>
Options
-f
,--filter
<packages...>
- Include specific packages.-e
,--exclude
<packages...>
- Exclude specific packages.-w
,--workspace
<workspaces...>
- Root workspaces of the packages.-s
,--save
- Install as dependencies.-d
,--dev
- Install as dev dependencies.-p
,--peer
- Install as peer dependencies.-o
,--optional
- Install as optional dependencies.
Examples
Basic Usage
Add lodash
to all packages in the monorepo.
monopkg add lodash
bun x monopkg add lodash
npx monopkg add lodash
yarn dlx monopkg add lodash
Comparison
The command above is equivalent to running the following commands for each package in the monorepo:
cd ./packages/pkg-a && bun add lodash
cd ./packages/pkg-b && bun add lodash
cd ./packages/pkg-c && bun add lodash
cd ./packages/pkg-d && bun add lodash
- and so on.
Output
{
"dependencies": {
"lodash": "^4.17.21",
"some-package": "^1.0.0"
}
}
{
"dependencies": {
"lodash": "^4.17.21",
"some-package": "^1.0.0"
}
}
{
"dependencies": {
"lodash": "^4.17.21",
"some-package": "^1.0.0"
}
}
With Inclusion Filters
Add lodash
to package-a
and package-b
.
monopkg add lodash -f package-a package-b
bun x monopkg add lodash -f package-a package-b
npx monopkg add lodash -f package-a package-b
yarn dlx monopkg add lodash -f package-a package-b
Comparison
The command above is equivalent to running the following commands:
cd ./packages/package-a && bun add lodash
cd ./packages/package-b && bun add lodash
With Target Environments
Add lodash
and typescript
as devDependencies
to all packages in the monorepo.
monopkg add -d lodash typescript
bun x monopkg add -d lodash typescript
npx monopkg add -d lodash typescript
yarn dlx monopkg add -d lodash typescript
Comparison
The command above is equivalent to running the following commands for each package in the monorepo:
cd ./packages/pkg-a && bun add -d lodash typescript
cd ./packages/pkg-b && bun add -d lodash typescript
cd ./packages/pkg-c && bun add -d lodash typescript
cd ./packages/pkg-d && bun add -d lodash typescript
- and so on.
With Exclusion Filters
Add lodash
and typescript
as peerDependencies
to all packages in the monorepo, except package-a
.
monopkg add -p lodash typescript -e package-a
bun x monopkg add -p lodash typescript -e package-a
npx monopkg add -p lodash typescript -e package-a
yarn dlx monopkg add -p lodash typescript -e package-a
Comparison
The command above is equivalent to running the following commands for each package in the monorepo, except package-a
:
cd ./packages/pkg-b && bun add -p lodash typescript
cd ./packages/pkg-c && bun add -p lodash typescript
cd ./packages/pkg-d && bun add -p lodash typescript
- and so on.
With Target Workspaces
Add lodash
to all packages in the apps
workspace.
monopkg add lodash -w apps
bun x monopkg add lodash -w apps
npx monopkg add lodash -w apps
yarn dlx monopkg add lodash -w apps
Comparison
The command above is equivalent to running the following commands for each package in the apps
workspace:
cd ./apps/app-a && bun add lodash
cd ./apps/app-b && bun add lodash
cd ./apps/app-c && bun add lodash
cd ./apps/app-d && bun add lodash
- and so on.
Advanced Usage
Add eslint
and typesript
as devDependencies
to all packages in the apps
and utils
workspaces, except app-a
and util-a
.
monopkg add -d eslint typescript -w apps utils -e app-a util-a
bun x monopkg add -d eslint typescript -w apps utils -e app-a util-a
npx monopkg add -d eslint typescript -w apps utils -e app-a util-a
yarn dlx monopkg add -d eslint typescript -w apps utils -e app-a util-a
Comparison
The command above is equivalent to running the following commands:
cd ./apps/app-b && bun add -d eslint typescript
cd ./apps/app-c && bun add -d eslint typescript
cd ./apps/app-d && bun add -d eslint typescript
cd ./utils/util-b && bun add -d eslint typescript
cd ./utils/util-c && bun add -d eslint typescript