Skip to content

fix: handle "main": false in package.json without crashing - #210

Merged
antfu merged 1 commit into
antfu:mainfrom
clanzhang:fix/main-false-crash
Sep 16, 2026
Merged

antfu merged 1 commit into
antfu:mainfrom
clanzhang:fix/main-false-crash

Conversation

@clanzhang

@clanzhang clanzhang commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

🔗 Linked issue

Fixes #209

🧭 Context

Some packages set "main": false in their package.json — [email protected] and [email protected] do this (both come in through get-intrinsic). They're exports-only packages.

analyzePackageModuleType() assumed main was always a string and called pkgJson.main?.endsWith(...) on it. Since false is not a string, this throws: TypeError: pkgJson.main?.endsWith is not a function The whole inspector goes down — UI shows "Failed to Connect to the Backend".

📚 Description

Normalize main to string | undefined at the top of the function:

const main = typeof pkgJson.main === 'string' ? pkgJson.main : undefined 

Then use the local variable everywhere. Four call sites changed from pkgJson.main?.endsWith to main?.endsWith.

Files changed:

  1. packages/node-modules-tools/src/analyze-esm.ts --- the fix
  2. packages/node-modules-tools/src/analyze-esm.test.ts --- test case with
    main: false

@pkg-pr-new

pkg-pr-new Bot commented Sep 16, 2026

Copy link
Copy Markdown

Open in StackBlitz

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

commit: 5808c80

@antfu
antfu merged commit 602140f into antfu:main Sep 16, 2026
11 checks passed
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.

Crash on packages that declare "main": false - pkgJson.main?.endsWith is not a function

2 participants