Repository navigation
Switching to esm-only - #2464
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
|
I did hit some color breakages in tests when I removed The underlying issue was testing output from |
|
|
||
| const program = new CommandWithTrace('program') | ||
| .option('-v, ---verbose') | ||
| .option('-v, --verbose') |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 deprecatedcommander/esm.mjsentry 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.stringifyfor consistent output - Replaced
__filename/__dirnamewithimport.meta.filename/import.meta.dirname - Updated Node.js engine requirement to
^20.19.0 || >=22.12.0to ensurerequire(esm)andimport.meta.dirnamesupport - Converted configuration files (eslint.config.js, .prettierrc.js) to ESM
- Deleted jest.config.js (already using Node's test runner)
- Used
createRequireworkaround 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.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…s into feature/esm-only
|
Lots of minor changes to convert implementation and examples and units tests from CommonJS to ESM! |
This comment was marked as outdated.
This comment was marked as outdated.
|
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 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:
Time to reread those articles about moving to ESM and think some more. 😅 |
|
(Updated original post with more background and explanation.) |
| // node alias.js print file | ||
| // node alias.js pr file | ||
| // node alias.js show file | ||
| // node alias.cjs --help |
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
Note: I could have kept the "default" (whole) import, but somewhat unusual these days. I switched to more common named import style.
|
Noting a change in approach for mocking imports in tests. I initially used |
|
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. |
|
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: |
|
Released in v15: https://github.com/tj/commander.js/releases/tag/v15.0.0 |
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) |  |  | --- ### 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 (\[[#​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 (\[[#​2405](tj/commander.js#2405)]) - update example to use compatible character for MINGW64 (\[[#​2475](tj/commander.js#2475)]) ##### Changed - *Breaking:* migrated Commander implementation from CommonJS to ESM (\[[#​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 (\[[#​2463](tj/commander.js#2463)]) ##### Deleted - *Breaking:* removed deprecated export of `commander/esm.mjs` (\[[#​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
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) |  |  | --- ### 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 (\[[#​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 (\[[#​2405](tj/commander.js#2405)]) - update example to use compatible character for MINGW64 (\[[#​2475](tj/commander.js#2475)]) ##### Changed - *Breaking:* migrated Commander implementation from CommonJS to ESM (\[[#​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 (\[[#​2463](tj/commander.js#2463)]) ##### Deleted - *Breaking:* removed deprecated export of `commander/esm.mjs` (\[[#​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
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) |  |  | --- ### 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 (\[[#​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 (\[[#​2405](tj/commander.js#2405)]) - update example to use compatible character for MINGW64 (\[[#​2475](tj/commander.js#2475)]) ##### Changed - *Breaking:* migrated Commander implementation from CommonJS to ESM (\[[#​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 (\[[#​2463](tj/commander.js#2463)]) ##### Deleted - *Breaking:* removed deprecated export of `commander/esm.mjs` (\[[#​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
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 supportsrequire(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.)
commander/esm.mjsentry pointpackage.jsonToDo:
package.jsontype tomodulerequire(esm)examples worthwhile??If we proceed with this PR:
(Note: can try out Jest examples by doing
npm packin Commander directory and installing the resulting tar file into example folder, to overwrite the non-existent 15.0.0.)ChangeLog
Changed
^20.19.0 || >=22.12.0forrequire(esm).Deleted
commander/esm.mjs