Skip to content

Switching to esm-only - #2464

Merged
shadowspawn merged 24 commits into
tj:release/15.xfrom
shadowspawn:feature/esm-only
Feb 19, 2026
Merged

shadowspawn merged 24 commits into
tj:release/15.xfrom
shadowspawn:feature/esm-only

Conversation

@shadowspawn

@shadowspawn shadowspawn commented Dec 31, 2025 •

Copy link
Copy Markdown
Collaborator

Problem

Commander is written in CommonJS with shims for ESM imports and types, and a complex exports in package.json. It would be nice to move to ESM now that node supports require(esm).

We have a relatively low overhead CommonJS implementation with the ESM shim. This also avoids the dual-package hazard. However moving forward, there will be more situations where there is friction to support CommonJS implementation in ESM-first tooling (e.g. #2345 (comment)).

With require(esm) now available in Node.js, and Deno and Bun having supported this for some time, there is solid runtime support for ESM-only dependencies. Hurrah! A major blocker removed.

Some tooling still requires extra work for ESM, with Jest being a common example. Also a familiar example, because we were using it in Commander ourselves! To be fair on Jest, it is stuck waiting for some API issues to be worked through in Node.js, and even upstream in V8. Some popular libraries went ESM-only years ago so the work required to support ESM-only is fairly well established.

Some ESM-only related links:

Packages gone ESM-only:

See: #2345

Solution

Go ESM-only. Document migration tips for common scenarios. Note that Commander 14 will be supported until (approximately) May 2027 so users do not need to update now. (And support for ESM-only is likely to continue to improve in the meantime making an update easier.)

  • switch implementation files from CommonJS to ESM
  • drop the shim files
  • delete deprecated commander/esm.mjs entry point
  • simplify exports in package.json
  • add Migration Tips in CHANGELOG as important part of migration

ToDo:

  • investigate whether FORCE_COLOR still needed where was added for breakage in Jest
  • change package.json type to module
  • convert tests to ESM
  • convert (most) example files to ESM
  • convert README examples to ESM
  • plan for impact on legacy setups, such as CommonJS projects using Jest (familiar example since that was us!)
  • are the Jest require(esm) examples worthwhile??

If we proceed with this PR:

  • add pinned issue announcing plan
  • make prerelease available after this PR lands
  • update commander dependency in jest examples to prerelease version

(Note: can try out Jest examples by doing npm pack in Commander directory and installing the resulting tar file into example folder, to overwrite the non-existent 15.0.0.)

ChangeLog

Changed

  • Breaking: Migrated Commander implementation from CommonJS to pure ESM (ECMAScript Modules). This is expected to be seamless for ESM consumers, but CommonJS consumers may hit issues with tooling requiring configuration for ESM-only dependencies.
  • Breaking: Commander 15 requires Node.js v20 or higher. To be more precise, ^20.19.0 || >=22.12.0 for require(esm).

Deleted

  • Breaking: removed deprecated export of commander/esm.mjs

@shadowspawn

This comment was marked as resolved.

@shadowspawn shadowspawn added the semver: major Releasing requires a major version bump, not backwards compatible label Jan 3, 2026
@shadowspawn

Copy link
Copy Markdown
Collaborator Author

I did hit some color breakages in tests when I removed FORCE_COLOR, so some of the color problems were not due to Jest.

The underlying issue was testing output from console.log(array) which does some coloring of array elements based on their type. I added a JSON.stringify() wrapper to log plain text.


const program = new CommandWithTrace('program')
.option('-v, ---verbose')
.option('-v, --verbose')

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For interest: I was running each of the example files after updating, and the current version of commander picked up the error in the long option.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request migrates Commander.js from CommonJS to pure ESM (ECMAScript Modules). The change leverages Node.js's support for require(esm) to maintain backwards compatibility with CommonJS consumers while adopting a modern ESM-first approach.

Changes:

  • Converted all implementation files from CommonJS (require/module.exports) to ESM (import/export)
  • Removed ESM shim files (esm.mjs, typings/esm.d.mts) and deprecated commander/esm.mjs entry point
  • Updated package.json to set "type": "module" and simplified exports configuration
  • Converted all test files and example files to ESM syntax
  • Updated fixture files to use JSON.stringify for consistent output
  • Replaced __filename/__dirname with import.meta.filename/import.meta.dirname
  • Updated Node.js engine requirement to ^20.19.0 || >=22.12.0 to ensure require(esm) and import.meta.dirname support
  • Converted configuration files (eslint.config.js, .prettierrc.js) to ESM
  • Deleted jest.config.js (already using Node's test runner)
  • Used createRequire workaround in tests that need to mock CommonJS modules

Reviewed changes

Copilot reviewed 171 out of 173 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
package.json Updated to ESM with "type": "module", simplified exports, bumped Node version requirement
index.js Converted to ESM with named exports
lib/*.js Converted all library files from CommonJS to ESM
tests/*.js Converted all tests to ESM, using createRequire for mocking where needed
examples/* Converted examples to ESM imports
typings/index.d.ts Updated documentation examples to use import.meta instead of CommonJS globals
typings/esm.d.mts Deleted (no longer needed)
esm.mjs Deleted (no longer needed)
jest.config.js Deleted (using Node test runner)
docs/deprecated.md Moved commander/esm.mjs from deprecated to removed section
eslint.config.js, .prettierrc.js Converted to ESM exports

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread examples/configure-help.js Resolved
Comment thread lib/command.js Resolved
@shadowspawn
shadowspawn marked this pull request as ready for review January 17, 2026 00:04
@shadowspawn

Copy link
Copy Markdown
Collaborator Author

Lots of minor changes to convert implementation and examples and units tests from CommonJS to ESM!

@shadowspawn

This comment was marked as outdated.

@shadowspawn
shadowspawn marked this pull request as draft January 17, 2026 22:37
@shadowspawn

shadowspawn commented Jan 17, 2026 •

Copy link
Copy Markdown
Collaborator Author

Well drat. Reality check. Nothing surprising, but I was being overly optimistic about impact.

I tried using this build of commander in an existing CommonJS project which uses TypeScript and Jest. No problem running program. So that part worked due to Node.js support for require(esm)!

But Jest in the consuming project blows up because sees an import in a CommonJS context due to the esm-only implementation of commander. Sigh. Consumers still have problems if they are using Jest, or other tooling that is not yet ESM friendly.

Possible options:

  • We could stick with esm-only moving forward, but perhaps extend support for Commander v14 as the version to use if your project setup does not support esm-only, and perhaps provide example changes for Jest.
  • We could transpile the esm to CommonJS for a dual build as an interim solution so CommonJS consumers can use latest Commander without upgrading their infrastructure .
  • We could abandon conversion and postpone move to esm for years.

Time to reread those articles about moving to ESM and think some more. 😅

@shadowspawn

Copy link
Copy Markdown
Collaborator Author

(Updated original post with more background and explanation.)

Comment thread examples/alias.cjs
// node alias.js print file
// node alias.js pr file
// node alias.js show file
// node alias.cjs --help

@shadowspawn shadowspawn Jan 23, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I wanted some CommonJS examples still. No particular reason it was alias.js.


const commander = require('commander');
const program = new commander.Command();
import { Command, Argument } from 'commander';

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I could have kept the "default" (whole) import, but somewhat unusual these days. I switched to more common named import style.

@shadowspawn

Copy link
Copy Markdown
Collaborator Author

Noting a change in approach for mocking imports in tests. I initially used createRequire to allow mocking some node library methods from ESM. Since we can freely mix and match CommonJS and ESM in tests, I switched the affected files back to CommonJS (.cjs) rather than needing the work-around. (Mocking of ESM is expected to come to node:test eventually.)

@shadowspawn

Copy link
Copy Markdown
Collaborator Author

I have added examples of Jest tests with changes for ESM-only Commander. I have mixed feelings about including specific migration examples, but some people are going to hit them, and we were using Jest, so trying it out.

@shadowspawn
shadowspawn marked this pull request as ready for review February 14, 2026 04:11
@shadowspawn

shadowspawn commented Feb 14, 2026 •

Copy link
Copy Markdown
Collaborator Author

While I was researching whether to go to ESM-only, I was reassured to see these two past articles from 2021 recommending dual support:

And the same two authors now moving to esm-only:

@shadowspawn
shadowspawn merged commit f4bd470 into tj:release/15.x Feb 19, 2026
9 checks passed
@shadowspawn
shadowspawn deleted the feature/esm-only branch February 19, 2026 05:07
@shadowspawn shadowspawn added the pending release Merged into a branch for a future release, but not released yet label Feb 19, 2026
@shadowspawn shadowspawn removed the pending release Merged into a branch for a future release, but not released yet label May 29, 2026
@shadowspawn

Copy link
Copy Markdown
Collaborator Author

huskas-2189 pushed a commit to huskas-2189/Bookmark that referenced this pull request Jun 14, 2026
This PR contains the following updates:

| Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) |
|---|---|---|---|
| [commander](https://github.com/tj/commander.js) | [`14.0.3` → `15.0.0`](https://renovatebot.com/diffs/npm/commander/14.0.3/15.0.0) | ![age](https://developer.mend.io/api/mc/badges/age/npm/commander/15.0.0?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/commander/14.0.3/15.0.0?slim=true) |

---

### Release Notes

<details>
<summary>tj/commander.js (commander)</summary>

### [`v15.0.0`](https://github.com/tj/commander.js/blob/HEAD/CHANGELOG.md#1500-2026-05-29)

[Compare Source](tj/commander.js@v14.0.3...v15.0.0)

Commander 15 is ESM only. This is expected to be seamless for ESM consumers, but some CommonJS consumers may hit issues with tooling requiring configuration for ESM-only dependencies. See Migration Tips below.

The release of Commander 15 moves Commander 14 into maintenance. Commander 14 will get security updates for
12 months (to May 2027). For more info see [Release Policy](./docs/release-policy.md).

##### Added

- show excess command-arguments in error message (\[[#&#8203;2384](tj/commander.js#2384)])

##### Fixed

- *Breaking:* only lone `--no-*` option sets default option value to `true`, default not implicitly set when define both positive and negative option in either order (\[[#&#8203;2405](tj/commander.js#2405)])
- update example to use compatible character for MINGW64 (\[[#&#8203;2475](tj/commander.js#2475)])

##### Changed

- *Breaking:* migrated Commander implementation from CommonJS to ESM (\[[#&#8203;2464](tj/commander.js#2464)])
- *Breaking:* Commander 15 requires Node.js v22.12.0 or higher (for `require(esm)`).
- dev: switch tests from Jest to `node:test` test runner (\[[#&#8203;2463](tj/commander.js#2463)])

##### Deleted

- *Breaking:* removed deprecated export of `commander/esm.mjs` (\[[#&#8203;2464](tj/commander.js#2464)])

##### Migration Tips

Commander 15 is ESM only, but this does not mean you need to migrate to ESM to use it. Importing ESM from CommonJS is
supported by Node.js, and Bun, and Deno. Hopefully it Just Works for you! However, you may be using a different runtime or
some other part of your setup that may not yet natively support importing ESM from CommonJS, such as your testing framework
or bundler.

If you have problems using Commander 15 in your environment, one option is stay on Commander 14 for now. Commander 14 will
get security updates until May 2027 and things will hopefully improve for your setup in the meantime.

</details>

---

### Configuration

📅 **Schedule**: (UTC)

- Branch creation
  - At any time (no schedule defined)
- Automerge
  - At any time (no schedule defined)

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4xOTUuMCIsInVwZGF0ZWRJblZlciI6IjQzLjE5NS4wIiwidGFyZ2V0QnJhbmNoIjoiZGV2ZWxvcCIsImxhYmVscyI6W119-->

Reviewed-on: https://codeberg.org/huskas-2189/Bookmark/pulls/85
huskas-2189 pushed a commit to huskas-2189/Bookmark that referenced this pull request Jun 19, 2026
This PR contains the following updates:

| Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) |
|---|---|---|---|
| [commander](https://github.com/tj/commander.js) | [`14.0.3` → `15.0.0`](https://renovatebot.com/diffs/npm/commander/14.0.3/15.0.0) | ![age](https://developer.mend.io/api/mc/badges/age/npm/commander/15.0.0?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/commander/14.0.3/15.0.0?slim=true) |

---

### Release Notes

<details>
<summary>tj/commander.js (commander)</summary>

### [`v15.0.0`](https://github.com/tj/commander.js/blob/HEAD/CHANGELOG.md#1500-2026-05-29)

[Compare Source](tj/commander.js@v14.0.3...v15.0.0)

Commander 15 is ESM only. This is expected to be seamless for ESM consumers, but some CommonJS consumers may hit issues with tooling requiring configuration for ESM-only dependencies. See Migration Tips below.

The release of Commander 15 moves Commander 14 into maintenance. Commander 14 will get security updates for
12 months (to May 2027). For more info see [Release Policy](./docs/release-policy.md).

##### Added

- show excess command-arguments in error message (\[[#&#8203;2384](tj/commander.js#2384)])

##### Fixed

- *Breaking:* only lone `--no-*` option sets default option value to `true`, default not implicitly set when define both positive and negative option in either order (\[[#&#8203;2405](tj/commander.js#2405)])
- update example to use compatible character for MINGW64 (\[[#&#8203;2475](tj/commander.js#2475)])

##### Changed

- *Breaking:* migrated Commander implementation from CommonJS to ESM (\[[#&#8203;2464](tj/commander.js#2464)])
- *Breaking:* Commander 15 requires Node.js v22.12.0 or higher (for `require(esm)`).
- dev: switch tests from Jest to `node:test` test runner (\[[#&#8203;2463](tj/commander.js#2463)])

##### Deleted

- *Breaking:* removed deprecated export of `commander/esm.mjs` (\[[#&#8203;2464](tj/commander.js#2464)])

##### Migration Tips

Commander 15 is ESM only, but this does not mean you need to migrate to ESM to use it. Importing ESM from CommonJS is
supported by Node.js, and Bun, and Deno. Hopefully it Just Works for you! However, you may be using a different runtime or
some other part of your setup that may not yet natively support importing ESM from CommonJS, such as your testing framework
or bundler.

If you have problems using Commander 15 in your environment, one option is stay on Commander 14 for now. Commander 14 will
get security updates until May 2027 and things will hopefully improve for your setup in the meantime.

</details>

---

### Configuration

📅 **Schedule**: (UTC)

- Branch creation
  - At any time (no schedule defined)
- Automerge
  - At any time (no schedule defined)

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4xOTUuMCIsInVwZGF0ZWRJblZlciI6IjQzLjE5NS4wIiwidGFyZ2V0QnJhbmNoIjoiZGV2ZWxvcCIsImxhYmVscyI6W119-->

Reviewed-on: https://codeberg.org/huskas-2189/Bookmark/pulls/85
huskas-2189 pushed a commit to huskas-2189/Bookmark that referenced this pull request Jun 19, 2026
This PR contains the following updates:

| Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) |
|---|---|---|---|
| [commander](https://github.com/tj/commander.js) | [`14.0.3` → `15.0.0`](https://renovatebot.com/diffs/npm/commander/14.0.3/15.0.0) | ![age](https://developer.mend.io/api/mc/badges/age/npm/commander/15.0.0?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/commander/14.0.3/15.0.0?slim=true) |

---

### Release Notes

<details>
<summary>tj/commander.js (commander)</summary>

### [`v15.0.0`](https://github.com/tj/commander.js/blob/HEAD/CHANGELOG.md#1500-2026-05-29)

[Compare Source](tj/commander.js@v14.0.3...v15.0.0)

Commander 15 is ESM only. This is expected to be seamless for ESM consumers, but some CommonJS consumers may hit issues with tooling requiring configuration for ESM-only dependencies. See Migration Tips below.

The release of Commander 15 moves Commander 14 into maintenance. Commander 14 will get security updates for
12 months (to May 2027). For more info see [Release Policy](./docs/release-policy.md).

##### Added

- show excess command-arguments in error message (\[[#&#8203;2384](tj/commander.js#2384)])

##### Fixed

- *Breaking:* only lone `--no-*` option sets default option value to `true`, default not implicitly set when define both positive and negative option in either order (\[[#&#8203;2405](tj/commander.js#2405)])
- update example to use compatible character for MINGW64 (\[[#&#8203;2475](tj/commander.js#2475)])

##### Changed

- *Breaking:* migrated Commander implementation from CommonJS to ESM (\[[#&#8203;2464](tj/commander.js#2464)])
- *Breaking:* Commander 15 requires Node.js v22.12.0 or higher (for `require(esm)`).
- dev: switch tests from Jest to `node:test` test runner (\[[#&#8203;2463](tj/commander.js#2463)])

##### Deleted

- *Breaking:* removed deprecated export of `commander/esm.mjs` (\[[#&#8203;2464](tj/commander.js#2464)])

##### Migration Tips

Commander 15 is ESM only, but this does not mean you need to migrate to ESM to use it. Importing ESM from CommonJS is
supported by Node.js, and Bun, and Deno. Hopefully it Just Works for you! However, you may be using a different runtime or
some other part of your setup that may not yet natively support importing ESM from CommonJS, such as your testing framework
or bundler.

If you have problems using Commander 15 in your environment, one option is stay on Commander 14 for now. Commander 14 will
get security updates until May 2027 and things will hopefully improve for your setup in the meantime.

</details>

---

### Configuration

📅 **Schedule**: (UTC)

- Branch creation
  - At any time (no schedule defined)
- Automerge
  - At any time (no schedule defined)

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4xOTUuMCIsInVwZGF0ZWRJblZlciI6IjQzLjE5NS4wIiwidGFyZ2V0QnJhbmNoIjoiZGV2ZWxvcCIsImxhYmVscyI6W119-->

Reviewed-on: https://codeberg.org/huskas-2189/Bookmark/pulls/85
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

semver: major Releasing requires a major version bump, not backwards compatible

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants