--filter (or -F) flag is used for selecting packages by pattern in a monorepo. Patterns can be used to match package names or package paths, with full glob syntax support.
Currently --filter is supported by bun install and bun outdated, and can also be used to run scripts for multiple packages at once.
Matching
Package Name --filter <pattern>
Name patterns select packages based on the package name, as specified in package.json. For example, if you have packages pkg-a, pkg-b and other, you can match all packages with *, only pkg-a and pkg-b with pkg*, and a specific package by providing the full name of the package.
Package Path --filter ./<glob>
Path patterns are specified by starting the pattern with ./, and will select all packages in directories that match the pattern. For example, to match all packages in subdirectories of packages, you can use --filter './packages/**'. To match a package located in packages/foo, use --filter ./packages/foo.
bun install and bun outdated
Both bun install and bun outdated support the --filter flag.
bun install by default will install dependencies for all packages in the monorepo. To install dependencies for specific packages, use --filter.
Given a monorepo with workspaces pkg-a, pkg-b, and pkg-c under ./packages:
terminal
bun outdated will display outdated dependencies for all packages in the monorepo, and --filter can be used to restrict the command to a subset of the packages:
terminal
bun install and bun outdated.
Running scripts with --filter
Use the --filter flag to execute scripts in multiple packages at once:
terminal
packages/api and packages/frontend, both with a dev script that will start a local development server. Normally, you would have to open two separate terminal tabs, cd into each package directory, and run bun dev:
terminal
--filter, you can run the dev script in both packages at once:
terminal
Running scripts in workspaces
Filters respect your workspace configuration: If you have apackage.json file that specifies which packages are part of the workspace,
--filter will be restricted to only these packages. Also, in a workspace you can use --filter to run scripts in packages that are located anywhere in the workspace:
terminal
Dependency Order
Bun will respect package dependency order when running scripts. Say you have a packagefoo that depends on another package bar in your workspace, and both packages have a build script. When you run bun --filter '*' build, you will notice that foo will only start running once bar is done.