@berenddeboer/nx-knip
v1.1.0
Published
Nx self-inferring plugin for Knip - find unused dependencies, exports, and files
Maintainers
Readme
@berenddeboer/nx-knip
A self-inferring Nx plugin for Knip - find unused dependencies, exports, and files in your JavaScript/TypeScript projects.
Install
Install both the plugin and knip:
npm install --save-dev @berenddeboer/nx-knip knippnpm add --save-dev @berenddeboer/nx-knip knipyarn add --dev @berenddeboer/nx-knip knipbun add -D @berenddeboer/nx-knip knipUsage
Plugin Setup
Add the plugin to your nx.json:
{
"plugins": [
{
"plugin": "@berenddeboer/nx-knip",
"options": {
"targetName": "knip"
}
}
]
}This will automatically add a knip target to all projects in your workspace.
Running Knip
Run knip for a single project:
nx knip my-projectRun knip for all affected projects:
nx affected -t knipOr run it for all projects:
nx run-many -t knipExecutor Options
strict
Enable strict mode (implies --production). Defaults to false. When enabled, knip will:
- Verify isolation: workspaces should use strictly their own dependencies
- Include
peerDependencieswhen finding unused or unlisted dependencies - Report type-only imports listed in
dependencies
You can enable it for all projects via the plugin options in nx.json:
{
"plugins": [
{
"plugin": "@berenddeboer/nx-knip",
"options": {
"targetName": "knip",
"strict": true
}
}
]
}Or per-project in project.json:
{
"targets": {
"knip": {
"options": {
"strict": true
}
}
}
}env
Set environment variables for the knip process. This is useful when knip loads .ts files that throw on missing environment variables - you can provide dummy values so knip can analyze the files without errors.
Configure via targetDefaults in nx.json:
{
"targetDefaults": {
"knip": {
"options": {
"env": {
"SQLITE_DATABASE_PATH": "/tmp/knip-dummy.db",
"PF_ORG": "examples/demo"
}
}
}
}
}Or per-project in project.json:
{
"targets": {
"knip": {
"options": {
"env": {
"DATABASE_URL": "postgres://localhost/dummy"
}
}
}
}
}The provided environment variables are merged with the current process.env, so existing variables are preserved and the specified ones are added or overridden.
How it works
This plugin runs knip --workspace {projectRoot} from the workspace root for each project. This means:
- It uses the
--workspaceflag to focus on a specific workspace - The workspace root
knip.jsonconfiguration is used - If Bun is installed, the plugin automatically uses
knip-bunfor significantly better performance
