Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"require() of ES modules is not supported" when linting a commit #902

Closed
1 of 4 tasks
danielway opened this issue Jan 23, 2020 · 9 comments · Fixed by sourcefuse/react-boilerplate-ts-ui#35
Closed
1 of 4 tasks

Comments

@danielway
Copy link

danielway commented Jan 23, 2020

Expected Behavior

commitlint should lint commit messages without errors.

Current Behavior

In a project whose package.json is configured as "type": "module", conventional-changelog/commitlint produces the following error:

(node:27380) Warning: require() of ES modules is not supported.
require() of [FULL PATH REDACTED]/commitlint-esm-bug-repro/commitlint.config.js from
[FULL PATH REDACTED]/commitlint-esm-bug-repro/node_modules/cosmiconfig/node_modules/import-fresh/index.js
is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which
defines all .js files in that package scope as ES modules.
Instead rename commitlint.config.js to end in .cjs, change the requiring code to use import(), or remove
"type": "module" from [FULL PATH REDACTED]/commitlint-esm-bug-repro/package.json.

Affected packages

  • cli
  • core
  • prompt
  • config-angular

Possible Solution

This seems related to how the underlying cosmiconfig package is loading the commitlint configuration.

Steps to Reproduce (for bugs)

Reproduction repository: https://github.com/danielway/commitlint-esm-bug-repro

  1. Clone and install the above repository
  2. Create some file, add to git, and commit the change
  3. Observe the error listed above

Your Environment

Commitlint: 8.3.5
Node: 12.14.1, 13.1.0, and 13.7.0 all exhibit this behavior

@danielway danielway changed the title "require() of ES modules is not supported" when commiting "require() of ES modules is not supported" when linting a commit Jan 23, 2020
@armano2
Copy link
Contributor

armano2 commented Jan 26, 2020

in theory you should be able to use --config flag to provide cjs version of config

commitlint --config commitlint.config.cjs -E HUSKY_GIT_PARAMS

but this is not going to work as cosmiconfig is not supporting cjs files cosmiconfig/cosmiconfig#224


note: this can be fixed within @commitlint/loader by adding loader for those files
cosmiconfig.loaders

@marionebl
Copy link
Contributor

This is interesting, thanks for reporting. We use cosmiconfig to load our configuration. I think this should be solved centrally by them. As @armano2 has pointed out the issue is tracked over at cosmiconfig/cosmiconfig#224. Closing in favor of said issue

@FloodGames
Copy link

FloodGames commented Apr 23, 2020

note: this can be fixed within @commitlint/loader by adding loader for those files
cosmiconfig.loaders

How?
I try to run node-modules through Babel (that's what you mean right?) - doesn't work.

@make-github-pseudonymous-again
Copy link

How does one workaround this issue?

PS: OK, I have tried passing the config with a .cjs file extension in commitlint --config .commitlintrc.cjs and it does work.
PPS: I guess this is the PR that fixed it cosmiconfig/cosmiconfig#238
PPPS: If you used .commitlintrc.js without pointing to it with the --config flag it is sufficient to rename the file to .commitlintrc.cjs.

@RomanistHere
Copy link

Warning: Instead rename commitlint.config.js to end in .cjs - didn't work, after I simply changed .js to .cjs but

PS: OK, I have tried passing the config with a .cjs file extension in commitlint --config .commitlintrc.cjs and it does work. PPS: I guess this is the PR that fixed it davidtheclark/cosmiconfig#238 PPPS: If you used .commitlintrc.js without pointing to it with the --config flag it is sufficient to rename the file to .commitlintrc.cjs.

Changed my npx --no-install commitlint --edit $1 string in .husky/commit-msg to npx --no-install commitlint --config commitlint.config.cjs --edit $1 - it worked. Thanks.

@escapedcat
Copy link
Member

@RomanistHere I assume this will work after this is merged: #2797

@houtan-rocky
Copy link

This problem still exists

/Users/base/Repos/marginex-frontend/node_modules/commitlint/node_modules/@commitlint/cli/lib/cli.js:100
        throw err;
        ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/base/Repos/marginex-frontend/node_modules/@commitlint/config-conventional/lib/index.js from /Users/base/Repos/marginex-frontend/node_modules/commitlint/node_modules/@commitlint/resolve-extends/lib/index.js not supported.
Instead change the require of /Users/base/Repos/marginex-frontend/node_modules/@commitlint/config-conventional/lib/index.js in /Users/base/Repos/marginex-frontend/node_modules/commitlint/node_modules/@commitlint/resolve-extends/lib/index.js to a dynamic import() which is available in all CommonJS modules.
    at /Users/base/Repos/marginex-frontend/node_modules/commitlint/node_modules/@commitlint/resolve-extends/lib/index.js:42:19
    at Array.reduce (<anonymous>)
    at loadExtends (/Users/base/Repos/marginex-frontend/node_modules/commitlint/node_modules/@commitlint/resolve-extends/lib/index.js:39:16)
    at Object.resolveExtends [as default] (/Users/base/Repos/marginex-frontend/node_modules/commitlint/node_modules/@commitlint/resolve-extends/lib/index.js:25:22)
    at Object.load [as default] (/Users/base/Repos/marginex-frontend/node_modules/commitlint/node_modules/@commitlint/load/lib/load.js:38:47) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v21.5.0
husky - commit-msg script failed (code 1)

@jack3898
Copy link

jack3898 commented Apr 6, 2024

I feel like this is a regression as I have updated my commitlint recently to 19.1.0, reverting to 18.5.0 fixes the issue for me. The above amendments mentioned to the bash file did not work.

@aaronknott

This comment was marked as duplicate.

danielzeljko added a commit to danielzeljko/nextjs-cookiecutter that referenced this issue Apr 12, 2024
blu3eee added a commit to blu3eee/nextjs-templates that referenced this issue Apr 30, 2024
blu3eee added a commit to blu3eee/nextjs-templates that referenced this issue Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

10 participants