-
Notifications
You must be signed in to change notification settings - Fork 1
Fix: Handle package.json exports field for sub-path imports (#47) #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Adding CLAUDE.md with task information for AI processing. This file will be removed when the task is complete. Issue: #47
This commit fixes the issue where use-m fails to resolve sub-path imports
like 'yargs/helpers' when they are defined in the package.json exports field.
## Root Cause
When importing packages with sub-paths (e.g., 'yargs/helpers'), use-m was:
1. Reading package.json from the wrong location (subpath directory instead of package root)
2. Only checking for the root "." export, not sub-path exports like "./helpers"
## Changes Made
1. Updated `tryResolveModule` in both npm and bun resolvers to:
- Accept `packageRootPath` parameter to read package.json from correct location
- Check for sub-path exports in the exports field before falling back to root export
- Support both dotted and non-dotted sub-path keys (e.g., "./helpers" and "/helpers")
2. Added `resolveExportsTarget` helper function to handle both:
- String export values
- Conditional export objects (with import, default, require, etc.)
3. Updated both use.mjs and use.js with the fix
## Testing
- Created reproduction test in experiments/test-yargs-helpers.mjs
- Added comprehensive test suite in tests/exports-field.test.mjs
- Verified fix works with yargs/helpers and multiple versions
- Tested hideBin functionality to ensure proper import
## Example
```javascript
// Now works correctly:
const { hideBin } = await use('yargs/helpers');
const yargs17 = await use('[email protected]/helpers');
const yargs18 = await use('[email protected]/helpers');
```
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
Apply the same sub-path exports field resolution to use.cjs (CommonJS version) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
This reverts commit e1675f3.
🤖 Solution Draft LogThis log file contains the complete execution trace of the AI solution draft process. 💰 Cost estimation:
Now working session is ended, feel free to review and add any feedback on the solution draft. |
🔄 Auto-restart 1/3Detected uncommitted changes from previous run. Starting new session to review and commit them. Uncommitted files: Auto-restart will stop after changes are committed or after 2 more iterations. Please wait until working session will end and give your feedback. |
The package-lock.json was updated with peer dependency flags for compatibility. This is a routine lockfile update from npm install/test operations. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
🤖 Solution Draft LogThis log file contains the complete execution trace of the AI solution draft process. 💰 Cost estimation:
Now working session is ended, feel free to review and add any feedback on the solution draft. |
Summary
This PR fixes issue #47 where use-m fails to resolve sub-path imports like
yargs/helperswhen they are defined in the package.jsonexportsfield.Problem
When trying to import a package sub-path like
yargs/helpers, use-m was failing with:Root Cause
The issue had two parts:
Solution
Updated both
npmandbunresolvers inuse.mjs,use.js, anduse.cjsto:Added
resolveExportsTargethelper - Handles both string exports and conditional export objects (import, default, require, etc.)Updated
tryResolveModulefunction signature - Now accepts:modulePath: The full path to resolvepackageRootPath: The package root for reading package.jsonsubPath: The sub-path portion (e.g., "/helpers") for exports field lookupEnhanced exports field resolution:
"./helpers": "./helpers/helpers.mjs")Testing
Reproduction Test
Created
experiments/test-yargs-helpers.mjsthat successfully imports both:yargs(main package)yargs/helpers(sub-path export)Comprehensive Test Suite
Added
tests/exports-field.test.mjswith tests for:yargs/helpersimport with exports field[email protected]/helpers,[email protected]/helpers)Results
Files Changed
use.mjs- ES module version with fixuse.js- Browser/universal version with fixuse.cjs- CommonJS version with fixtests/exports-field.test.mjs- New comprehensive test suiteexperiments/- Debug and reproduction scriptsFixes
Closes #47
🤖 Generated with Claude Code
Co-Authored-By: Claude [email protected]