Skip to content

Conversation

@konard
Copy link
Member

@konard konard commented Nov 7, 2025

Summary

This PR fixes issue #47 where use-m fails to resolve sub-path imports like yargs/helpers when they are defined in the package.json exports field.

Problem

When trying to import a package sub-path like yargs/helpers, use-m was failing with:

Error: Failed to resolve the path to 'yargs/helpers' from '/path/node_modules/yargs-v-latest/helpers'.

Root Cause

The issue had two parts:

  1. Wrong package.json location: Reading package.json from the sub-path directory instead of the package root
  2. Missing sub-path exports handling: Only checking for the root "." export, not sub-path exports like "./helpers"

Solution

Updated both npm and bun resolvers in use.mjs, use.js, and use.cjs to:

  1. Added resolveExportsTarget helper - Handles both string exports and conditional export objects (import, default, require, etc.)

  2. Updated tryResolveModule function signature - Now accepts:

    • modulePath: The full path to resolve
    • packageRootPath: The package root for reading package.json
    • subPath: The sub-path portion (e.g., "/helpers") for exports field lookup
  3. Enhanced exports field resolution:

    • Reads package.json from the package root (not sub-path directory)
    • Checks for sub-path exports first (e.g., "./helpers": "./helpers/helpers.mjs")
    • Falls back to root "." export if no sub-path match
    • Properly resolves mapped file paths relative to package root

Testing

Reproduction Test

Created experiments/test-yargs-helpers.mjs that successfully imports both:

  • yargs (main package)
  • yargs/helpers (sub-path export)

Comprehensive Test Suite

Added tests/exports-field.test.mjs with tests for:

  • yargs/helpers import with exports field
  • Multiple versions ([email protected]/helpers, [email protected]/helpers)
  • Actual functionality testing (hideBin function)
  • @octokit/core as additional validation

Results

// ✅ Now works correctly:
const { hideBin } = await use('yargs/helpers');
const yargs17 = await use('[email protected]/helpers');
const yargs18 = await use('[email protected]/helpers');

Files Changed

  • use.mjs - ES module version with fix
  • use.js - Browser/universal version with fix
  • use.cjs - CommonJS version with fix
  • tests/exports-field.test.mjs - New comprehensive test suite
  • experiments/ - Debug and reproduction scripts

Fixes

Closes #47

🤖 Generated with Claude Code

Co-Authored-By: Claude [email protected]

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #47
@konard konard self-assigned this Nov 7, 2025
konard and others added 2 commits November 7, 2025 05:49
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]>
@konard konard changed the title [WIP] Cannot import sub-paths like 'yargs/helpers' Fix: Handle package.json exports field for sub-path imports (#47) Nov 7, 2025
@konard konard marked this pull request as ready for review November 7, 2025 05:54
@konard
Copy link
Member Author

konard commented Nov 7, 2025

🤖 Solution Draft Log

This log file contains the complete execution trace of the AI solution draft process.

💰 Cost estimation:

  • Public pricing estimate: $5.737890 USD
  • Calculated by Anthropic: $3.017131 USD
  • Difference: $-2.720759 (-47.42%)
    📎 Log file uploaded as GitHub Gist (602KB)
    🔗 View complete solution draft log

Now working session is ended, feel free to review and add any feedback on the solution draft.

@konard
Copy link
Member Author

konard commented Nov 7, 2025

🔄 Auto-restart 1/3

Detected uncommitted changes from previous run. Starting new session to review and commit them.

Uncommitted files:

M package-lock.json

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]>
@konard
Copy link
Member Author

konard commented Nov 7, 2025

🤖 Solution Draft Log

This log file contains the complete execution trace of the AI solution draft process.

💰 Cost estimation:

  • Public pricing estimate: unknown
  • Calculated by Anthropic: $0.934392 USD
  • Difference: unknown
    📎 Log file uploaded as GitHub Gist (885KB)
    🔗 View complete solution draft log

Now working session is ended, feel free to review and add any feedback on the solution draft.

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.

Cannot import sub-paths like 'yargs/helpers'

2 participants