Monorepos & Workspaces
Workspaces are handled out-of-the-box by Knip.
Workspaces are sometimes also referred to as package-based monorepos, or as
packages in a monorepo. Knip uses the term workspace exclusively to indicate a
directory that has a package.json
.
Configuration
Here’s example configuration with custom entry
and project
patterns:
Each workspace has the same default configuration.
The root workspace is named "."
under workspaces
(like in the example
above).
Workspaces
Knip reads workspaces from four possible locations:
- The
workspaces
array inpackage.json
(npm, Bun, Yarn, Lerna) - The
packages
array inpnpm-workspace.yaml
(pnpm) - The
workspaces.packages
array inpackage.json
(legacy) - The
workspaces
object in Knip configuration
The workspaces
in Knip configuration (4) not already defined in the root
package.json
or pnpm-workspace.yaml
(1, 2, 3) are added to the analysis.
For projects with only a root package.json
, please see integrated
monorepos.
Additional options
The following options are available inside workspace configurations:
Plugins can be configured separately per workspace.
Use --debug
for verbose output and see the workspaces Knip includes, their
configurations, enabled plugins, glob options and resolved files.
Lint a single workspace
Use the --workspace
(or -W
) argument to focus on a single workspace (and let
Knip run faster). Example:
This will include ancestor and dependent workspaces, for two reasons:
- Ancestor workspaces may list dependencies in
package.json
the linted workspace uses. - Dependent workspaces may reference exports from the linted workspace.
To lint the workspace in isolation, you can combine this with strict production mode.
ISC License © 2024 Lars Kappert