|
| 1 | +--- |
| 2 | +id: finders |
| 3 | +title: Finders |
| 4 | +--- |
| 5 | + |
| 6 | +Added in: v10.16.0 |
| 7 | + |
| 8 | +Finder functions let you **search your dependency graph** by any property of a package, not just its name. |
| 9 | +They can be declared in [.pnpmfile.cjs] and used with [pnpm list] and [pnpm why]. |
| 10 | + |
| 11 | +[.pnpmfile.cjs]: ./pnpmfile.md |
| 12 | +[pnpm list]: ./cli/list.md |
| 13 | +[pnpm why]: ./cli/why.md |
| 14 | + |
| 15 | +## Defining finder functions |
| 16 | + |
| 17 | +Finder functions are declared in your project’s [.pnpmfile.cjs] file under the finders export. |
| 18 | +Each function receives a context object and must return either: |
| 19 | + |
| 20 | +* `true` → include this dependency in the results, |
| 21 | +* `false` → skip it, |
| 22 | +* or a `string` → include this dependency and print the string as additional info. |
| 23 | + |
| 24 | +Example: a finder that matches any dependency with **React 17** in `peerDependencies`: |
| 25 | + |
| 26 | +```js title=".pnpmfile.cjs" |
| 27 | +module.exports = { |
| 28 | + finders: { |
| 29 | + react17: (ctx) => { |
| 30 | + return ctx.readManifest().peerDependencies?.react === "^17.0.0" |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | +``` |
| 35 | +
|
| 36 | +### Finder context (ctx) |
| 37 | +
|
| 38 | +Each finder function receives a context object that describes the dependency node being visited. |
| 39 | +
|
| 40 | +|Field|Type/Example|Description| |
| 41 | +|--|--|--| |
| 42 | +|`name`|`"minimist"`|Package name.| |
| 43 | +|`version`|`"1.2.8"`|Package version.| |
| 44 | +|`readManifest()`|returns the `package.json` object|Load the package manifest (use this for fields like `peerDependencies`, `license`, `engines`, etc.).| |
| 45 | +
|
| 46 | +## Using finders |
| 47 | +
|
| 48 | +You can invoke a finder with the `--find-by=<functionName>` flag: |
| 49 | +
|
| 50 | +``` |
| 51 | +pnpm why --find-by=react17 |
| 52 | +``` |
| 53 | +
|
| 54 | +Output: |
| 55 | +
|
| 56 | +``` |
| 57 | +@apollo/client 4.0.4 |
| 58 | +├── @graphql-typed-document-node/core 3.2.0 |
| 59 | +└── graphql-tag 2.12.6 |
| 60 | +``` |
| 61 | +
|
| 62 | +## Returning extra metadata |
| 63 | +
|
| 64 | +A finder can also return a string. That string will be shown alongside the matched package in the output. |
| 65 | +
|
| 66 | +Example: print the package license: |
| 67 | +
|
| 68 | +```js |
| 69 | +module.exports = { |
| 70 | + finders: { |
| 71 | + react17: (ctx) => { |
| 72 | + const manifest = ctx.readManifest() |
| 73 | + if (manifest.peerDependencies?.react === "^17.0.0") { |
| 74 | + return `license: ${manifest.license}` |
| 75 | + } |
| 76 | + return false |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | +
|
| 82 | +Output: |
| 83 | +
|
| 84 | +``` |
| 85 | +@apollo/client 4.0.4 |
| 86 | +├── @graphql-typed-document-node/core 3.2.0 |
| 87 | +│ license: MIT |
| 88 | +└── graphql-tag 2.12.6 |
| 89 | + license: MIT |
| 90 | +``` |
| 91 | +
|
| 92 | +Othere example use cases: |
| 93 | +* Find all packages with a specific license. |
| 94 | +* Detect packages requiring a minimum Node.js version. |
| 95 | +* List all dependencies that expose binaries. |
| 96 | +* Print funding URLs for all packages. |
| 97 | +
|
0 commit comments