Skip to content

fix: resolve use-m ESM/CJS interop for yargs on Node.js v23+ - #56

Open
fredericgermain wants to merge 2 commits into
link-assistant:mainfrom
fredericgermain:fix/use-m-esm-cjs-interop
Open

fix: resolve use-m ESM/CJS interop for yargs on Node.js v23+#56
fredericgermain wants to merge 2 commits into
link-assistant:mainfrom
fredericgermain:fix/use-m-esm-cjs-interop

Conversation

@fredericgermain

@fredericgermain fredericgermain commented May 10, 2026

Copy link
Copy Markdown

Problem

Closes #55

claude-profiles fails on Node.js v23+ (and v24) with errors like:

```
TypeError: hideBin is not a function
TypeError: yargs is not a function
TypeError: archiver is not a function
```

Root Cause

use-m dynamically loads CJS packages into an ESM context. On newer Node.js versions, it wraps default exports under a .default property instead of returning them directly. This affects callable default exports (yargs, archiver) and named exports accessed via helpers (yargs/helpers).

Fix

Apply defensive .default unwrapping to all four use-m loaded packages:

```diff
-const [{ $ }, _yargs, yargsHelpers, archiver] = await Promise.all([
+const [_commandStream, _yargs, _yargsHelpers, _archiver] = await Promise.all([
use('command-stream@0.7.0'),
use('yargs@17.7.2'),
use('yargs@17.7.2/helpers'),
use('archiver@7.0.1')
]);

-// use-m wraps CJS modules under .default in ESM context; unwrap defensively
+// use-m wraps CJS modules under .default in ESM context on Node.js v23+; unwrap defensively
+// command-stream exports $ as a named export at the top level (not under .default)
+const { $ } = _commandStream;
const yargs = (typeof _yargs === 'function') ? _yargs : (_yargs?.default ?? _yargs);
-const hideBin = yargsHelpers?.hideBin ?? yargsHelpers?.default?.hideBin ?? ((argv) => argv.slice(2));
+const hideBin = _yargsHelpers?.hideBin ?? _yargsHelpers?.default?.hideBin ?? ((argv) => argv.slice(2));
+const archiverFn = (typeof _archiver === 'function') ? _archiver : (_archiver?.default ?? _archiver);
```

Module Pattern Reason
command-stream const { $ } = _commandStream $ is a named export at top level — no .default needed
yargs typeof fn ? fn : fn.default ?? fn Callable default export wrapped under .default
yargs/helpers x.hideBin ?? x.default.hideBin ?? fallback Named export hideBin is under .default
archiver typeof fn ? fn : fn.default ?? fn Callable default export wrapped under .default

Testing

Verified on Node.js v24.12.0 — claude-profiles --store global and claude-profiles --help work correctly after the fix.

fredericgermain and others added 2 commits May 10, 2026 07:42
Extends the Node.js v23+ compatibility fix from the initial commit to cover
all four use-m loaded packages, not just yargs:

- archiver: same .default unwrap pattern as yargs (it's a callable default export)
- yargsHelpers: rename to _yargsHelpers for consistency
- command-stream: rename to _commandStream; $ is a named export at top level,
  so no .default unwrap needed (verified empirically)

Also adds archiverFn as the resolved callable, replacing direct archiver() calls
in saveProfileSilent and saveProfile.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

claude-profiles --list fails at v1.3.5

1 participant