Skip to content

fix: fallback to LICENSE file when package.json has no license field - #204

Open
clanzhang wants to merge 1 commit into
antfu:mainfrom
clanzhang:fix/detect-license-from-file
Open

fix: fallback to LICENSE file when package.json has no license field#204
clanzhang wants to merge 1 commit into
antfu:mainfrom
clanzhang:fix/detect-license-from-file

Conversation

@clanzhang

Copy link
Copy Markdown
Contributor

Some packages like require-like don't declare a license field in package.json but ship a LICENSE file.
Read common license file names and infer the SPDX identifier from well-known license texts.

Closes #145

🔗 Linked issue

#145

🧭 Context

Some older packages like require-like don't declare a license field in
their package.json (this was common before npm standardized the field).
Instead, they ship a LICENSE file with the actual license text.

Currently, normalizePkgLicense() only reads from package.json, so these
packages show up as having no license at all in the inspector.

📚 Description

When package.json has no license or licenses field, fall back to
scanning common license file names (LICENSE, LICENSE.md, LICENCE, etc.)
in the package directory and infer the SPDX identifier from well-known
license text patterns.

Changes:

  • Added detectLicenseFromFile() in package-json.ts that reads license
    files and matches text patterns for MIT, ISC, Apache-2.0, BSD-2-Clause,
    BSD-3-Clause, and UPL-1.0
  • Updated resolvePackage() in resolve.ts to call the fallback when
    normalizePkgLicense() returns undefined
  • Added unit tests covering all supported license types and edge cases

Not changed:

  • The registry-based resolver (registry/resolve.ts) is unaffected — it
    has no filesystem access
  • normalizePkgLicense() itself is untouched; the fallback is only applied
    in the filesystem-based resolvePackage()

@pkg-pr-new

pkg-pr-new Bot commented Sep 7, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/node-modules-inspector@204
npm i https://pkg.pr.new/node-modules-tools@204

commit: 184b3f3

@clanzhang
clanzhang force-pushed the fix/detect-license-from-file branch from 24f6e54 to 184b3f3 Compare September 11, 2026 09:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fails to detect MIT license in require-like package

1 participant