Filtering
Filtering allows you to restrict commands to specific subsets of packages.
pnpm supports a rich selector syntax for picking packages by name or byrelation.
Selectors may be specified via the--filter
(or-F
) flag:
pnpm --filter <package_selector> <command>
Matching
--filter <package_name>
To select an exact package, just specify its name (@scope/pkg
) or use apattern to select a set of packages (@scope/*
).
Examples:
pnpm --filter "@babel/core" test
pnpm --filter "@babel/*" test
pnpm --filter "*core" test
Specifying the scope of the package is optional, so--filter=core
will pick@babel/core
ifcore
is not found.However, if the workspace has multiple packages with the same name (for instance,@babel/core
and@types/core
),then filtering without scope will pick nothing.
--filter <package_name>...
To select a package and its dependencies (direct and non-direct), suffix thepackage name with an ellipsis:<package_name>...
. For instance, the nextcommand will run tests offoo
and all of its dependencies:
pnpm --filter foo... test
You may use a pattern to select a set of root packages:
pnpm --filter "@babel/preset-*..." test
--filter <package_name>^...
To ONLY select the dependencies of a package (both direct and non-direct),suffix the name with the aforementioned ellipsis preceded by a chevron. Forinstance, the next command will run tests for all offoo
'sdependencies:
pnpm --filter "foo^..." test
--filter ...<package_name>
To select a package and its dependent packages (direct and non-direct), prefixthe package name with an ellipsis:...<package_name>
. For instance, this willrun the tests offoo
and all packages dependent on it:
pnpm --filter ...foo test
--filter "...^<package_name>"
To ONLY select a package's dependents (both direct and non-direct), prefix thepackage name with an ellipsis followed by a chevron. For instance, this willrun tests for all packages dependent onfoo
:
pnpm --filter "...^foo" test
--filter./<glob>
, --filter{<glob>}
A glob pattern relative to the current working directory matching projects.
pnpm --filter "./packages/**" <cmd>
Includes all projects that are under the specified directory.
It may be used with the ellipsis and chevron operators to selectdependents/dependencies as well:
pnpm --filter ...{<directory>} <cmd>
pnpm --filter {<directory>}... <cmd>
pnpm --filter ...{<directory>}... <cmd>
It may also be combined with[<since>]
. For instance, to select all changedprojects inside a directory:
pnpm --filter "{packages/**}[origin/master]" <cmd>
pnpm --filter "...{packages/**}[origin/master]" <cmd>
pnpm --filter "{packages/**}[origin/master]..." <cmd>
pnpm --filter "...{packages/**}[origin/master]..." <cmd>
Or you may select all packages from a directory with names matching the givenpattern:
pnpm --filter "@babel/*{components/**}" <cmd>
pnpm --filter "@babel/*{components/**}[origin/master]" <cmd>
pnpm --filter "...@babel/*{components/**}[origin/master]" <cmd>
--filter "[<since>]"
Selects all the packages changed since the specified commit/branch. May besuffixed or prefixed with...
to include dependencies/dependents.
For example, the next command will run tests in all changed packages sincemaster
and on any dependent packages:
pnpm --filter "...[origin/master]" test
--fail-if-no-match
Use this flag if you want the CLI to fail if no packages have matched the filters.
Excluding
Any of the filter selectors may work as exclusion operators when they have aleading "!". In zsh (and possibly other shells), "!" should be escaped:\!
.
For instance, this will run a command in all projects except forfoo
:
pnpm --filter=!foo <cmd>
And this will run a command in all projects that are not under thelib
directory:
pnpm --filter=!./lib <cmd>
Multiplicity
When packages are filtered, every package is taken that matches at least one ofthe selectors. You can use as many filters as you want:
pnpm --filter ...foo --filter bar --filter baz... test
--filter-prod <filtering_pattern>
Acts the same a--filter
but omitsdevDependencies
when selecting dependency projectsfrom the workspace.
--test-pattern <glob>
test-pattern
allows detecting whether the modified files are related to tests.If they are, the dependent packages of such modified packages are not included.
This option is useful with the "changed since" filter. For instance, the nextcommand will run tests in all changed packages, and if the changes are in thesource code of the package, tests will run in the dependent packages as well:
pnpm --filter="...[origin/master]" --test-pattern="test/*" test
--changed-files-ignore-pattern <glob>
Allows to ignore changed files by glob patterns when filtering for changed projects since the specified commit/branch.
Usage example:
pnpm --filter="...[origin/master]" --changed-files-ignore-pattern="**/README.md" run build