question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
make-github-pseudonymous-againcommented, Jun 1, 2021

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 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.

9reactions
RomanistHerecommented, Oct 19, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found