conventional-changelog / commitlint

📓 Lint commit messages
https://commitlint.js.org
MIT License
16.84k stars 905 forks source link

Can not use @commitlint/cz-commitlint as commitizen's adapter #3949

Closed wangys4dev closed 8 months ago

wangys4dev commented 8 months ago

Steps to Reproduce

1. setup scripts and install packages:

{
  "scripts": {
    "cz": "cz",
  },
  "devDependencies": {
    "@commitlint/cli": "^19.0.3",
    "@commitlint/config-conventional": "^19.0.3",
    "@commitlint/cz-commitlint": "^19.0.3",
    "commitizen": "^4.3.0"
  }
}

2. configs:
```jsonc
// .commitlintrc.json
{
  "extends": ["@commitlint/config-conventional"]
}
// .cz.json
{
  "path": "@commitlint/cz-commitlint"
}
  1. run pnpm cz

Current Behavior

got error

require() of ES Module C:\Users\admin\workspace\admin-template-vue\node_modules\.pnpm\@commitlint+cz-commitlint@19.0.3_@types+node@20.11.24_commitizen@4.3.0_inquirer@9.2.15_typescript@5.3.3\node_modules\@commitlint\cz-commitlint\lib\index.js from C:\Users\admin\workspace\admin-template-vue\node_modules\.pnpm\commitizen@4.3.0_@types+node@20.11.24_typescript@5.3.3\node_modules\commitizen\dist\commitizen\adapter.js not supported.
Instead change the require of index.js in C:\Users\admin\workspace\admin-template-vue\node_modules\.pnpm\commitizen@4.3.0_@types+node@20.11.24_typescript@5.3.3\node_modules\commitizen\dist\commitizen\adapter.js to a dynamic import() which is available in all CommonJS modules.
 ELIFECYCLE  Command failed with exit code 1.

Expected Behavior

Run commitizen normally

Affected packages

Possible Solution

3850

Context

No response

commitlint --version

@commitlint/cli@19.0.3

git --version

git version 2.44.0.windows.1

node --version

v20.11.1

risu729 commented 8 months ago

I also faced the same issue. This issue may be related. https://github.com/commitizen/cz-cli/issues/916

frantic1048 commented 8 months ago

Update - March 13, 2024: The workaround described below is no longer necessary. Upgrade @commitlint/cz-commitlint to version 19.1.0 or higher and it will function correctly with commitizen

Version 19.1.0 of @commitlint/cz-commitlint has added the required CJS export for commitizen.

For details, see the release notes at conventional-changelog/commitlint v19.1.0.


Workaround for ESM version of commitlint:

Create an adapter module: cz-adapter.cjs:

exports.prompter = async (inquirerIns, commit) => {
  ;(await import('@commitlint/cz-commitlint')).prompter(inquirerIns, commit)
}

In package.json:

{
  "config": {
    "commitizen": {
      "path": "./cz-adapter.cjs"
    }
  }
}
wangys4dev commented 8 months ago

@frantic1048 Thanks, it works for me.

barunprasad commented 8 months ago

@frantic1048 This worked for me as well. Thank you.

frantic1048 commented 7 months ago

As a reminder, upgrading @commitlint/cz-commitlint to 19.1.0 eliminates the need for the adapter workaround mentioned above, and commitizen will function as usual.