commitlint: ERR_REQUIRE_ESM when using Node.js ESM in tandem with TypeScript config (commitlint.config.ts)
// package.json
{
"type": "module"
}
// tsconfig.json
{
"compilerOptions": {
"module": "node16"
}
}
Expected Behavior
With the given setup above, executing npx commitlint --to HEAD should not result in ERR_REQUIRE_ESM error.
Current Behavior
Currently, the above setup is giving the following output:
./node_modules/ts-node/dist-raw/node-internal-errors.js:46
const err = new Error(getErrRequireEsmMessage(filename, parentPath, packageJsonPath))
^
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: ./commitlint.config.ts
require() of ES modules is not supported.
require() of ./commitlint.config.ts from ./node_modules/cosmiconfig-typescript-loader/dist/loader.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from ./package.json.
at createErrRequireEsm (./node_modules/ts-node/dist-raw/node-internal-errors.js:46:15)
at assertScriptCanLoadAsCJSImpl (./node_modules/ts-node/dist-raw/node-internal-modules-cjs-loader.js:584:11)
at Object.require.extensions.<computed> [as .ts] (./node_modules/ts-node/src/index.ts:1589:5)
at Module.load (node:internal/modules/cjs/loader:988:32)
at Function.Module._load (node:internal/modules/cjs/loader:834:12)
at Module.require (node:internal/modules/cjs/loader:1012:19)
at require (node:internal/modules/cjs/helpers:102:18)
at loader (./node_modules/cosmiconfig-typescript-loader/dist/loader.js:12:28)
at Explorer.loadFileContent (./node_modules/cosmiconfig/src/Explorer.ts:81:32)
at Explorer.createCosmiconfigResult (./node_modules/cosmiconfig/src/Explorer.ts:89:36) {
name: 'TypeScriptCompileError'
}
Affected packages
- cli
- core
- prompt
- config-angular
Possible Solution
- Support
commitlint.config.cts, or - Use
import()to load config
Steps to Reproduce (for bugs)
- First step: Minimal setup of
commitlintaccording to the official documentation withpackage.jsonandtsconfig.jsonsupplied above. - Second step: Run
npx commitlint --to HEAD.
commitlint.config.ts
import type { UserConfig } from "@commitlint/types";
const config: UserConfig = {
extends: ["@commitlint/config-conventional"],
};
export default config;
Context
Without touching tsconfig.json, if I remove "type": "module" from package.json, npx commitlint --to HEAD can be executed successfully.
But this is not plausible since I have migrated the whole repository to Node.js ESM since the release of TypeScript 4.7 officially supporting it.
Raising this issue as people with similar setup will encounter this as well.
Currently resorted to using commitlint.config.cjs, but still prefer to have commitlint.config.ts in the long run.
Your Environment
| Executable | Version |
|---|---|
commitlint --version |
17.0.2 |
git --version |
2.36.1 |
node --version |
18.4.0 |
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 13
- Comments: 28 (7 by maintainers)
We also have the same issue after migrating to ESM, can confirm that renaming
commitlint.config.jstocommitlint.config.cjsworks as a temporary fix@joberstein Very nicely done❤️ I tested
commitlint.configfiles ending with.jsand.tsand using theesmsyntaxexport default {}and they all worked fineI changed
module.exports =toexport defaultand it worked for me.@SalahAdDin Thanks for your feedback. Let’s close this for now.
Thank you very much!
https://github.com/conventional-changelog/commitlint/releases/tag/v18.4.2
https://github.com/stylelint/stylelint/pull/7329 seems to have fixed the problem. I don’t know if it helps.
for now, we can just set a package resolution override to
"cosmiconfig": "9.0.0-alpha.3"@ZerdoX-x thanks for digging and confirming. If there’s nothing we can do on our side we could close this again.
Happy for a PR
The issue persists, not sure why this got closed. Adding support for
.cjsis a different concern and not a solution to what this issue describes.I have a
commitlint.config.tsfile withpackage.jsoncontaining"type": "module"and receive the exact error mentioned by the author.(Renaming file to
commitlint.config.ctsworks fine.)It didn’t work for me.
This is my case, it is not a direct dependency, it is a
commitlintdependency, and testing the package from CLI I’m getting the bug:Neither to me.
It seems to be a bug related to
ts-node, and it seems to be unsolved yet, right now I had to remove"type": "module"frompackage.json, but It should not be necessary.Is there any development on this?
Thank you!
#3461 might help. It was just merged but not yet released.
Same problem and I’m agree with @shian15810 's described context.
I also prefer
tsextension for configure commitlint.