Fix: Remove ambiguous extensionless main field - #53
Merged
Conversation
lovell
approved these changes
Jan 9, 2026
lovell
left a comment
Owner
There was a problem hiding this comment.
Brilliant, thank you for fixing this.
Owner
|
v4.2.2 now available. |
Contributor
Author
|
FYI, and it doesn't invalidate this PR, but I still get the same error on my project. The issue occurs because import speakingurl from 'speakingurl'; // CommonJS package
import hepburn from 'hepburn'; // CommonJS package
import { pinyin } from 'pinyin-pro'; // Likely CommonJSWhen served directly to the browser without pre-bundling (which happens when the parent package is excluded from Vite's optimizer), browsers cannot handle the CJS/ESM interop. To bypass the issue, I'll add |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The current
package.jsonhas an extensionlessmainfield:When both
limax.mjsandlimax.cjsexist in the same directory, this creates ambiguity for module bundlers like esbuild (used by Vite's dependency pre-bundler). The extensionless path can cause incorrect module resolution, where the package is interpreted as CommonJS even when imported as ESM. This result in the following error when using Vite dev server:Solution
Remove the
mainfield entirely since the package already properly definesexportswith conditional imports:Modern tools (Node.js 12.20+, Webpack 5, Vite, etc.) prioritize
exportsovermain. For legacy tools that don't supportexports, Node.js will still resolve the module correctly since"type": "module"is declared.Impact
exportsconditional fieldsAlternatively, if maintaining the
mainfield for older tooling is desired, it should include an explicit extension (e.g.,"main": "./lib/limax.cjs"or"main": "./lib/limax.mjs").