Skip to content

require() of ES Module /xxx/commitlint.config.js from /xxx/node_modules/cosmiconfig/dist/loaders.js not supported. Instead change the require of commitlint.config.js in /xxx/node_modules/cosmiconfig/dist/loaders.js to a dynamic import() which is available in all CommonJS modules. #3768

Closed
@pfdgithub

Description

@pfdgithub

Expected Behavior

When I upgrade commitlint to the latest version in an ESModule project and trigger the commit-msg hook, the following error will appear:

> git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file -
[STARTED] Preparing lint-staged...
[COMPLETED] Preparing lint-staged...
[STARTED] Running tasks for staged files...
[STARTED] lint-staged.config.js — 76 files
[STARTED] src/**/* — 59 files
[STARTED] prettier -l -w
[COMPLETED] prettier -l -w
[COMPLETED] src/**/* — 59 files
[COMPLETED] lint-staged.config.js — 76 files
[COMPLETED] Running tasks for staged files...
[STARTED] Applying modifications from tasks...
[COMPLETED] Applying modifications from tasks...
[STARTED] Cleaning up temporary files...
[COMPLETED] Cleaning up temporary files...
/xxx/node_modules/cosmiconfig/dist/loaders.js:32
    if (parseJson === undefined) {
                                                          ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /xxx/commitlint.config.js from /xxx/node_modules/cosmiconfig/dist/loaders.js not supported.
Instead change the require of commitlint.config.js in /xxx/node_modules/cosmiconfig/dist/loaders.js to a dynamic import() which is available in all CommonJS modules.
    at module.exports (/xxx/node_modules/import-fresh/index.js:32:59)
    at loadJsSync (/xxx/node_modules/cosmiconfig/dist/loaders.js:17:12)
    at #loadConfiguration (/xxx/node_modules/cosmiconfig/dist/Explorer.js:92:30)
    at #readConfiguration (/xxx/node_modules/cosmiconfig/dist/Explorer.js:77:80)
    at async search (/xxx/node_modules/cosmiconfig/dist/Explorer.js:44:40)
    at async Explorer.search (/xxx/node_modules/cosmiconfig/dist/Explorer.js:71:20)
    at async loadConfig (/xxx/node_modules/@commitlint/load/lib/utils/load-config.js:45:19)
    at async load (/xxx/node_modules/@commitlint/load/lib/load.js:19:20)
    at async main (/xxx/node_modules/@commitlint/cli/lib/cli.js:189:20) {
  code: 'ERR_REQUIRE_ESM',
  filepath: '/xxx/commitlint.config.js'
}

Node.js v18.17.0
husky - commit-msg hook exited with code 1 (error)

package.json

{
  "type": "module",
  "devDependencies": {
    "@commitlint/cli": "^18.4.1",
    "@commitlint/config-conventional": "^18.4.0"
  }
}

commit-msg

#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx --no-install commitlint --edit "$1"

commitlint.config.js

export default {
  extends: ["@commitlint/config-conventional"],
};

cosmiconfig version

% npm list cosmiconfig

├─┬ @commitlint/cli@18.4.1
│ └─┬ @commitlint/load@18.4.1
│   ├─┬ cosmiconfig-typescript-loader@5.0.0
│   │ └── cosmiconfig@8.3.6 deduped
│   └── cosmiconfig@8.3.6
└─┬ @svgr/rollup@8.1.0
  ├─┬ @svgr/core@8.1.0
  │ └── cosmiconfig@8.3.6 deduped
  └─┬ @svgr/plugin-svgo@8.1.0
    └── cosmiconfig@8.3.6 deduped

Current Behavior

No response

Affected packages

  • cli
  • core
  • prompt
  • config-angular

Possible Solution

No response

Steps to Reproduce

1. First step
2. Second step

Context

No response

commitlint --version

@commitlint/cli@18.4.1

git --version

git version 2.39.2 (Apple Git-143)

node --version

v18.17.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions