"require() of ES modules is not supported" when linting a commit
See original GitHub issueExpected 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
- Clone and install the above repository
- Create some file, add to git, and commit the change
- 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
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:6 (2 by maintainers)
Top Results From Across the Web
ESLint - Error: Must use import to load ES Module
I think the problem is that you are trying to use the deprecated babel-eslint parser, last updated a year ago, which looks like...
Read more >referenceerror: module is not defined in es module scope this ...
However, when attempting to commit this code, the on-commit linter I have is giving me the following error: ReferenceError: module is not defined...
Read more >lint-staged - npm
npm install --save-dev lint-staged # requires further setup. $ git commit ✓ Preparing lint-staged... ❯ Running tasks for staged files.
Read more >Potluck - Subscriptions × ES Modules in Node × Chicken ...
Full Stack Developers Wes Bos and Scott Tolinski dive deep into web development topics, explaining how they work and talking about their own...
Read more >ES6 is the Node way to go - DEV Community
const express = require('express'); const app = express() ... In order to enable Node support for ES modules we need to tweak the ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
How does one workaround this issue?
PS: OK, I have tried passing the config with a
.cjs
file extension incommitlint --config .commitlintrc.cjs
and it does work. PPS: I guess this is the PR that fixed it https://github.com/davidtheclark/cosmiconfig/pull/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
.Warning:
Instead rename commitlint.config.js to end in .cjs
- didn’t work, after I simply changed.js
to.cjs
butChanged my
npx --no-install commitlint --edit $1
string in.husky/commit-msg
tonpx --no-install commitlint --config commitlint.config.cjs --edit $1
- it worked. Thanks.