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

Installation of the hook fails with Error [ERR_REQUIRE_ESM] #152

Closed
nils-van-zuijlen opened this issue Feb 28, 2024 · 12 comments
Closed

Installation of the hook fails with Error [ERR_REQUIRE_ESM] #152

nils-van-zuijlen opened this issue Feb 28, 2024 · 12 comments
Assignees
Labels
bug Something isn't working

Comments

@nils-van-zuijlen
Copy link

Installing with the following .pre-commit-config.yaml fails with an error.

default_language_version:
  node: "20.11.1"
repos:
  - repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
    rev: v9.12.0
    hooks:
      - id: commitlint
        stages: [commit-msg]
        additional_dependencies: ["@commitlint/config-angular"]

The error, with home path anonymized with $HOME:

[INFO] Installing environment for https://github.com/alessandrojcm/commitlint-pre-commit-hook.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
commitlint...............................................................Failed
- hook id: commitlint
- exit code: 1

$HOME/.cache/pre-commit/repo6u5oyhlx/node_env-20.11.1/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/cli/lib/cli.js:123
        throw err;
        ^

Error [ERR_REQUIRE_ESM]: require() of ES Module $HOME/.cache/pre-commit/repo6u5oyhlx/node_env-20.11.1/lib/node_modules/@commitlint/config-conventional/lib/index.js from $HOME/.cache/pre-commit/repo6u5oyhlx/node_env-20.11.1/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/resolve-extends/lib/index.js not supported.
Instead change the require of $HOME/.cache/pre-commit/repo6u5oyhlx/node_env-20.11.1/lib/node_modules/@commitlint/config-conventional/lib/index.js in $HOME/.cache/pre-commit/repo6u5oyhlx/node_env-20.11.1/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/resolve-extends/lib/index.js to a dynamic import() which is available in all CommonJS modules.
    at $HOME/.cache/pre-commit/repo6u5oyhlx/node_env-20.11.1/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/resolve-extends/lib/index.js:48:19
    at Array.reduce (<anonymous>)
    at loadExtends ($HOME/.cache/pre-commit/repo6u5oyhlx/node_env-20.11.1/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/resolve-extends/lib/index.js:45:16)
    at resolveExtends ($HOME/.cache/pre-commit/repo6u5oyhlx/node_env-20.11.1/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/resolve-extends/lib/index.js:25:22)
    at load ($HOME/.cache/pre-commit/repo6u5oyhlx/node_env-20.11.1/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/load/lib/load.js:42:52) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v20.11.1

Our commitlint.config.js:

const scope = [
  "admin",
  "deps",
  "i18n",
  "settings",
  // and other strings
];

const types = [
  "build",
  "chore",
  "ci",
  "docs",
  "feat",
  "fix",
  "perf",
  "refactor",
  "revert",
  "style",
  "test",
];

module.exports = {
  extends: ["@commitlint/config-angular"],
  rules: {
    "scope-enum": [2, "always", scope],
    "type-enum": [2, "always", types],
  },
};
@alessandrojcm
Copy link
Owner

Hi there, this a weird once since this module is not ESM nor it does any import or require. Do you have a package.json in your project by any chance? If so can you please share it?

@nils-van-zuijlen
Copy link
Author

I do not have a project.json as the project is python.

@nils-van-zuijlen
Copy link
Author

I found out that @commitlint/config-angular and other configs backed by their repo recently (in v19) switched to ESM, so it may be related.

https://github.com/conventional-changelog/commitlint/releases/tag/v19.0.0

@alessandrojcm
Copy link
Owner

I found out that @commitlint/config-angular and other configs backed by their repo recently (in v19) switched to ESM, so it may be related.

https://github.com/conventional-changelog/commitlint/releases/tag/v19.0.0

Yeah that's probably it, ESM interop is always fun. I guess I could modify this package to be ESM by adding type: module but that might have unintended consequences.

@mecampbellsoup
Copy link

mecampbellsoup commented Feb 29, 2024

Any workaround for the time being? Experiencing same thing, here's our config:

const Configuration = {
  extends: ['@commitlint/config-conventional'],
  rules: {
    "header-max-length": [2, "always", 72], // To show full header in github
    "subject-case": [2, "always", "sentence-case"], // To look consistent in auto-generated changelog
    "scope-enum": [2, "always"],
  },
  /*
   * Array of functions that return true if commitlint should ignore the given message.
   * Given array is merged with predefined functions, which consist of matchers like:
   *
   * - 'Merge pull request', 'Merge X into Y' or 'Merge branch X'
   * - 'Revert X'
   * - 'v1.2.3' (ie semver matcher)
   * - 'Automatic merge X' or 'Auto-merged X into Y'
   *
   * To see full list, check https://github.com/conventional-changelog/commitlint/blob/master/%40commitlint/is-ignored/src/defaults.ts.
   * To disable those ignores and run rules always, set `defaultIgnores: false` as shown below.
   */
  ignores: [(commit) => commit === ''],
  /*
   * Whether commitlint uses the default ignore rules, see the description above.
   */
  defaultIgnores: true,
  /*
   * Custom URL to show upon failure
   */
  helpUrl:
    'https://github.com/conventional-changelog/commitlint/#what-is-commitlint',
  /*
   * Custom prompt configs
   */
  prompt: {
    messages: {},
    questions: {
      type: {
        description: 'please input type:',
      },
    },
  },
};

module.exports = Configuration;
commitlint...............................................................Failed
- hook id: commitlint
- exit code: 1

/home/mcampbell/.cache/pre-commit/repo9t8os46x/node_env-default/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/cli/lib/cli.js:123
        throw err;
        ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/mcampbell/.cache/pre-commit/repo9t8os46x/node_env-default/lib/node_modules/@commitlint/config-conventional/lib/index.js from /home/mcampbell/.cache/pre-commit/repo9t8os46x/node_env-default/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/resolve-extends/lib/index.js not supported.
Instead change the require of /home/mcampbell/.cache/pre-commit/repo9t8os46x/node_env-default/lib/node_modules/@commitlint/config-conventional/lib/index.js in /home/mcampbell/.cache/pre-commit/repo9t8os46x/node_env-default/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/resolve-extends/lib/index.js to a dynamic import() which is available in all CommonJS modules.
    at /home/mcampbell/.cache/pre-commit/repo9t8os46x/node_env-default/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/resolve-extends/lib/index.js:48:19
    at Array.reduce (<anonymous>)
    at loadExtends (/home/mcampbell/.cache/pre-commit/repo9t8os46x/node_env-default/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/resolve-extends/lib/index.js:45:16)
    at resolveExtends (/home/mcampbell/.cache/pre-commit/repo9t8os46x/node_env-default/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/resolve-extends/lib/index.js:25:22)
    at load (/home/mcampbell/.cache/pre-commit/repo9t8os46x/node_env-default/lib/node_modules/commitlint-pre-commit-hook/node_modules/@commitlint/load/lib/load.js:42:52) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v21.6.2

@alessandrojcm
Copy link
Owner

Hey, could you both please try with this tag and see if that sorts the issue?

@strieflin
Copy link

Works for me with:

  - repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
    rev: esm # This is a workaround for https://github.com/alessandrojcm/commitlint-pre-commit-hook/issues/152
    hooks:
      - id: commitlint
        stages:
          - commit-msg
        additional_dependencies:
          - '@commitlint/config-conventional'

@mecampbellsoup
Copy link

Works for me too.

(k8s-billing-py3.11) (⎈|default:k8s-billing)mcampbell-1➜  ~/github/coreweave/k8s-billing : mc/fix-org-target ✘ :✚ ᐅ  git commit -m "refactor: Increase default log level to INFO"
[WARNING] The 'rev' field of repo 'https://github.com/alessandrojcm/commitlint-pre-commit-hook' appears to be a mutable reference (moving tag / branch).  Mutable references are never updated after first install and are not supported.  See https://pre-commit.com/#using-the-latest-version-for-a-repository for more details.  Hint: `pre-commit autoupdate` often fixes this.
[INFO] Initializing environment for https://github.com/alessandrojcm/commitlint-pre-commit-hook.
[INFO] Initializing environment for https://github.com/alessandrojcm/commitlint-pre-commit-hook:@commitlint/config-conventional.
check for added large files..............................................Passed
check python ast.........................................................Passed
check for case conflicts.................................................Passed
check for merge conflicts................................................Passed
check json...........................................(no files to check)Skipped
check yaml...............................................................Passed
trim trailing whitespace.................................................Passed
fix end of files.........................................................Passed
check for case conflicts.................................................Passed
black....................................................................Passed
isort....................................................................Passed
pycln....................................................................Passed
prettier.................................................................Passed
pyupgrade................................................................Passed
markdownlint-fix-docker..............................(no files to check)Skipped
[WARNING] The 'rev' field of repo 'https://github.com/alessandrojcm/commitlint-pre-commit-hook' appears to be a mutable reference (moving tag / branch).  Mutable references are never updated after first install and are not supported.  See https://pre-commit.com/#using-the-latest-version-for-a-repository for more details.  Hint: `pre-commit autoupdate` often fixes this.
[INFO] Installing environment for https://github.com/alessandrojcm/commitlint-pre-commit-hook.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
commitlint...............................................................Passed
[mc/fix-org-target 4c48f3a] refactor: Increase default log level to INFO
 2 files changed, 2 insertions(+), 2 deletions(-)

@steeltomato
Copy link

Wow, I was going to pop in here to let you know version pinning the config dependency, such as additional_dependencies: ['@commitlint/[email protected]'] is a viable temporary workaround... but Alessandro is already on the proper fix!

@alessandrojcm
Copy link
Owner

Great I'll see about mergin it into master, it should be fine since ESM is supposed to be able to import CJS but weird things tend to happen.

@morremeyer
Copy link

Just verified against 46ad8d1, works without any problems. Looking forward to the release.

Thanks for your work @alessandrojcm!

@alessandrojcm alessandrojcm self-assigned this Mar 1, 2024
@alessandrojcm alessandrojcm added the bug Something isn't working label Mar 1, 2024
@alessandrojcm
Copy link
Owner

Released on v9.13.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants