Skip to content

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.

bash
monopkg add [options] <dependencies...>
bash
bun x monopkg add [options] <dependencies...>
bash
npx monopkg add [options] <dependencies...>
bash
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.

bash
monopkg add lodash
bash
bun x monopkg add lodash
bash
npx monopkg add lodash
bash
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
json
{
  "dependencies": {
    "lodash": "^4.17.21",    
    "some-package": "^1.0.0"
  }
}
json
{
  "dependencies": {
    "lodash": "^4.17.21",    
    "some-package": "^1.0.0"
  }
}
json
{
  "dependencies": {
    "lodash": "^4.17.21",    
    "some-package": "^1.0.0"
  }
}

With Inclusion Filters

Add lodash to package-a and package-b.

bash
monopkg add lodash -f package-a package-b
bash
bun x monopkg add lodash -f package-a package-b
bash
npx monopkg add lodash -f package-a package-b
bash
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.

bash
monopkg add -d lodash typescript
bash
bun x monopkg add -d lodash typescript
bash
npx monopkg add -d lodash typescript
bash
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.

bash
monopkg add -p lodash typescript -e package-a
bash
bun x monopkg add -p lodash typescript -e package-a
bash
npx monopkg add -p lodash typescript -e package-a
bash
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.

bash
monopkg add lodash -w apps
bash
bun x monopkg add lodash -w apps
bash
npx monopkg add lodash -w apps
bash
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.

bash
monopkg add -d eslint typescript -w apps utils -e app-a util-a
bash
bun x monopkg add -d eslint typescript -w apps utils -e app-a util-a
bash
npx monopkg add -d eslint typescript -w apps utils -e app-a util-a
bash
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

Released under the MIT License.