Description
loadPackageManifest in packages/knip/src/manifest/helpers.ts uses hardcoded node_modules paths to resolve package manifests:
_require(join(dir, 'node_modules', packageName, 'package.json'))
This fails in Yarn PnP environments where there is no node_modules directory, causing peer dependency metadata (bin, peerDependencies, etc.) to not be read. As a result, binaries are reported as unlisted and dependencies as unused incorrectly.
The original code even had a TODO comment acknowledging uncertainty about the approach:
"Not sure what's the most efficient way to get a package.json, but this seems to do the job across package managers (npm, Yarn, pnpm)"
Proposed fix
Replace hardcoded node_modules path lookups with createRequire from node:module, which uses Node.js standard module resolution. Yarn PnP hooks into this resolution, so it works without node_modules.
I have a working implementation and will open a PR shortly.
Description
loadPackageManifestinpackages/knip/src/manifest/helpers.tsuses hardcodednode_modulespaths to resolve package manifests:This fails in Yarn PnP environments where there is no
node_modulesdirectory, causing peer dependency metadata (bin,peerDependencies, etc.) to not be read. As a result, binaries are reported as unlisted and dependencies as unused incorrectly.The original code even had a TODO comment acknowledging uncertainty about the approach:
Proposed fix
Replace hardcoded
node_modulespath lookups withcreateRequirefromnode:module, which uses Node.js standard module resolution. Yarn PnP hooks into this resolution, so it works withoutnode_modules.I have a working implementation and will open a PR shortly.