Skip to content

Package manifest resolution fails in Yarn PnP environments #1590

@sekyungk

Description

@sekyungk

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions