conventional-changelog / commitlint

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

ERR_REQUIRE_ESM when using Node.js ESM in tandem with TypeScript config (commitlint.config.ts) #3251

Closed shian15810 closed 10 months ago

shian15810 commented 2 years ago
// package.json
{
  "type": "module"
}
// tsconfig.json
{
  "compilerOptions": {
    "module": "node16"
  }
}

Expected Behavior

With the given setup above, executing npx commitlint --to HEAD should not result in ERR_REQUIRE_ESM error.

Current Behavior

Currently, the above setup is giving the following output:

./node_modules/ts-node/dist-raw/node-internal-errors.js:46
  const err = new Error(getErrRequireEsmMessage(filename, parentPath, packageJsonPath))
              ^
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: ./commitlint.config.ts
require() of ES modules is not supported.
require() of ./commitlint.config.ts from ./node_modules/cosmiconfig-typescript-loader/dist/loader.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from ./package.json.

    at createErrRequireEsm (./node_modules/ts-node/dist-raw/node-internal-errors.js:46:15)
    at assertScriptCanLoadAsCJSImpl (./node_modules/ts-node/dist-raw/node-internal-modules-cjs-loader.js:584:11)
    at Object.require.extensions.<computed> [as .ts] (./node_modules/ts-node/src/index.ts:1589:5)
    at Module.load (node:internal/modules/cjs/loader:988:32)
    at Function.Module._load (node:internal/modules/cjs/loader:834:12)
    at Module.require (node:internal/modules/cjs/loader:1012:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at loader (./node_modules/cosmiconfig-typescript-loader/dist/loader.js:12:28)
    at Explorer.loadFileContent (./node_modules/cosmiconfig/src/Explorer.ts:81:32)
    at Explorer.createCosmiconfigResult (./node_modules/cosmiconfig/src/Explorer.ts:89:36) {
  name: 'TypeScriptCompileError'
}

Affected packages

Possible Solution

  1. Support commitlint.config.cts, or
  2. Use import() to load config

Steps to Reproduce (for bugs)

  1. First step: Minimal setup of commitlint according to the official documentation with package.json and tsconfig.json supplied above.
  2. Second step: Run npx commitlint --to HEAD.
commitlint.config.ts ```ts import type { UserConfig } from "@commitlint/types"; const config: UserConfig = { extends: ["@commitlint/config-conventional"], }; export default config; ```

Context

Without touching tsconfig.json, if I remove "type": "module" from package.json, npx commitlint --to HEAD can be executed successfully.

But this is not plausible since I have migrated the whole repository to Node.js ESM since the release of TypeScript 4.7 officially supporting it.

Raising this issue as people with similar setup will encounter this as well.

Currently resorted to using commitlint.config.cjs, but still prefer to have commitlint.config.ts in the long run.

Your Environment

Executable Version
commitlint --version 17.0.2
git --version 2.36.1
node --version 18.4.0
escapedcat commented 2 years ago

Thanks for raising this

erayaydin commented 2 years ago

Same problem and I'm agree with @shian15810 's described context.

Executable Version
commitlint 17.0.3
git 2.36.1
node 18.3.0

I also prefer ts extension for configure commitlint.

jjbell150 commented 2 years ago

We also have the same issue after migrating to ESM, can confirm that renaming commitlint.config.js to commitlint.config.cjs works as a temporary fix

Mnigos commented 2 years ago

same issue here

ZerdoX-x commented 1 year ago

Renaming commitlint.config.ts to commitlint.config.cts doesn't work in my case:

/Users/user/path/node_modules/.pnpm/cosmiconfig@7.1.0/node_modules/cosmiconfig/src/ExplorerBase.ts:109
      throw new Error(
            ^
Error: No loader specified for extension ".cts"
    at Explorer.getLoaderEntryForFile (/Users/zerdox/p/zerdox/zerdox.dev/node_modules/.pnpm/cosmiconfig@7.1.0/node_modules/cosmiconfig/src/ExplorerBase.ts:109:13)
    at Explorer.loadFileContent (/Users/zerdox/p/zerdox/zerdox.dev/node_modules/.pnpm/cosmiconfig@7.1.0/node_modules/cosmiconfig/src/Explorer.ts:80:25)
    at Explorer.createCosmiconfigResult (/Users/zerdox/p/zerdox/zerdox.dev/node_modules/.pnpm/cosmiconfig@7.1.0/node_modules/cosmiconfig/src/Explorer.ts:89:36)
    at runLoad (/Users/zerdox/p/zerdox/zerdox.dev/node_modules/.pnpm/cosmiconfig@7.1.0/node_modules/cosmiconfig/src/Explorer.ts:104:33)
    at async cacheWrapper (/Users/zerdox/p/zerdox/zerdox.dev/node_modules/.pnpm/cosmiconfig@7.1.0/node_modules/cosmiconfig/src/cacheWrapper.ts:13:18)
    at async loadConfig (/Users/zerdox/p/zerdox/zerdox.dev/node_modules/.pnpm/@commitlint+load@17.3.0/node_modules/@commitlint/load/src/utils/load-config.ts:42:16)
    at async load (/Users/zerdox/p/zerdox/zerdox.dev/node_modules/.pnpm/@commitlint+load@17.3.0/node_modules/@commitlint/load/src/load.ts:25:17)
    at async main (/Users/zerdox/p/zerdox/zerdox.dev/node_modules/.pnpm/@commitlint+cli@17.3.0/node_modules/@commitlint/cli/src/cli.ts:214:17)
husky - commit-msg hook exited with code 1 (error)

So currently commitlint doesn't support .(ts|cts|mts|js|mjs) configs...

escapedcat commented 1 year ago

3461 might help. It was just merged but not yet released.

SalahAdDin commented 1 year ago

"ts-node": { "esm": true },

It didn't work for me.

This is my case, it is not a direct dependency, it is a commitlint dependency, and testing the package from CLI I'm getting the bug:

 echo "[PAP-2013] feat(lang): :arrow_up: add Polish language" | pnpm commit    

> react-ts-vite-template@0.1.0 commit /home/luisalaguna/Projects/react-ts-vite-template
> commitlint

/home/luisalaguna/Projects/react-ts-vite-template/node_modules/.pnpm/ts-node@10.9.1_xplfzyzpegygk3axf4z63vz544/node_modules/ts-node/dist-raw/node-internal-errors.js:46
  const err = new Error(getErrRequireEsmMessage(filename, parentPath, packageJsonPath))
              ^
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/luisalaguna/Projects/react-ts-vite-template/commitlint.config.ts
require() of ES modules is not supported.
require() of /home/luisalaguna/Projects/react-ts-vite-template/commitlint.config.ts from /home/luisalaguna/Projects/react-ts-vite-template/node_modules/.pnpm/cosmiconfig-typescript-loader@4.3.0_or2qrscwofpw4jwfbychkdzroy/node_modules/cosmiconfig-typescript-loader/dist/cjs/index.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from /home/luisalaguna/Projects/react-ts-vite-template/package.json.

    at createErrRequireEsm (/home/luisalaguna/Projects/react-ts-vite-template/node_modules/.pnpm/ts-node@10.9.1_xplfzyzpegygk3axf4z63vz544/node_modules/ts-node/dist-raw/node-internal-errors.js:46:15)
    at assertScriptCanLoadAsCJSImpl (/home/luisalaguna/Projects/react-ts-vite-template/node_modules/.pnpm/ts-node@10.9.1_xplfzyzpegygk3axf4z63vz544/node_modules/ts-node/dist-raw/node-internal-modules-cjs-loader.js:584:11)
    at Object.require.extensions.<computed> [as .ts] (/home/luisalaguna/Projects/react-ts-vite-template/node_modules/.pnpm/ts-node@10.9.1_xplfzyzpegygk3axf4z63vz544/node_modules/ts-node/src/index.ts:1610:5)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Function.Module._load (node:internal/modules/cjs/loader:878:12)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at loader (/home/luisalaguna/Projects/react-ts-vite-template/node_modules/.pnpm/cosmiconfig-typescript-loader@4.3.0_or2qrscwofpw4jwfbychkdzroy/node_modules/cosmiconfig-typescript-loader/dist/cjs/index.js:59:22)
    at Explorer.loadFileContent (/home/luisalaguna/Projects/react-ts-vite-template/node_modules/.pnpm/cosmiconfig@7.1.0/node_modules/cosmiconfig/src/Explorer.ts:81:32)
    at Explorer.createCosmiconfigResult (/home/luisalaguna/Projects/react-ts-vite-template/node_modules/.pnpm/cosmiconfig@7.1.0/node_modules/cosmiconfig/src/Explorer.ts:89:36) {
  name: 'TypeScriptCompileError'
}
 ELIFECYCLE  Command failed with exit code 1.

Renaming commitlint.config.ts to commitlint.config.cts doesn't work in my case:

/Users/user/path/node_modules/.pnpm/cosmiconfig@7.1.0/node_modules/cosmiconfig/src/ExplorerBase.ts:109
      throw new Error(
            ^
Error: No loader specified for extension ".cts"
    at Explorer.getLoaderEntryForFile (/Users/zerdox/p/zerdox/zerdox.dev/node_modules/.pnpm/cosmiconfig@7.1.0/node_modules/cosmiconfig/src/ExplorerBase.ts:109:13)
    at Explorer.loadFileContent (/Users/zerdox/p/zerdox/zerdox.dev/node_modules/.pnpm/cosmiconfig@7.1.0/node_modules/cosmiconfig/src/Explorer.ts:80:25)
    at Explorer.createCosmiconfigResult (/Users/zerdox/p/zerdox/zerdox.dev/node_modules/.pnpm/cosmiconfig@7.1.0/node_modules/cosmiconfig/src/Explorer.ts:89:36)
    at runLoad (/Users/zerdox/p/zerdox/zerdox.dev/node_modules/.pnpm/cosmiconfig@7.1.0/node_modules/cosmiconfig/src/Explorer.ts:104:33)
    at async cacheWrapper (/Users/zerdox/p/zerdox/zerdox.dev/node_modules/.pnpm/cosmiconfig@7.1.0/node_modules/cosmiconfig/src/cacheWrapper.ts:13:18)
    at async loadConfig (/Users/zerdox/p/zerdox/zerdox.dev/node_modules/.pnpm/@commitlint+load@17.3.0/node_modules/@commitlint/load/src/utils/load-config.ts:42:16)
    at async load (/Users/zerdox/p/zerdox/zerdox.dev/node_modules/.pnpm/@commitlint+load@17.3.0/node_modules/@commitlint/load/src/load.ts:25:17)
    at async main (/Users/zerdox/p/zerdox/zerdox.dev/node_modules/.pnpm/@commitlint+cli@17.3.0/node_modules/@commitlint/cli/src/cli.ts:214:17)
husky - commit-msg hook exited with code 1 (error)

So currently commitlint doesn't support .(ts|cts|mts|js|mjs) configs...

Neither to me.

It seems to be a bug related to ts-node, and it seems to be unsolved yet, right now I had to remove "type": "module" from package.json, but It should not be necessary.

Is there any development on this?

3461 might help. It was just merged but not yet released.

It would be pretty enough!

Thank you!

SalahAdDin commented 1 year ago

3461 might help. It was just merged but not yet released.

At least for me, It solved the problem.

escapedcat commented 1 year ago

@SalahAdDin Thanks for your feedback. Let's close this for now.

DamianGlowala commented 11 months ago

The issue persists, not sure why this got closed. Adding support for .cjs is a different concern and not a solution to what this issue describes.

I have a commitlint.config.ts file with package.json containing "type": "module" and receive the exact error mentioned by the author.

(Renaming file to commitlint.config.cts works fine.)

ZerdoX-x commented 11 months ago

Yep. Thanks for raising this up. I would also like commitlint to respect "type": "module" I set without switching from default .js/.ts extension.

escapedcat commented 11 months ago

Happy for a PR

alextnetto commented 11 months ago

I changed module.exports = to export default and it worked for me.

ZerdoX-x commented 11 months ago

@Alextnetto can confirm that @commitlint/cli@17.7.2

with following contents of commitlint.config.ts

import type { UserConfig } from "@commitlint/types";

const config: UserConfig = {
    extends: ["@commitlint/config-conventional"],
};

export default config;

still throws an error:

/project/node_modules/.pnpm/ts-node@10.9.1_@types+node@20.8.4_typescript@5.2.2/node_modules/ts-node/dist-raw/node-internal-errors.js:46
  const err = new Error(getErrRequireEsmMessage(filename, parentPath, packageJsonPath))
              ^
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /project/commitlint.config.ts
require() of ES modules is not supported.
require() of /home/zerdox/projects/job/imi/tonna_company-profile/commitlint.config.ts from /project/node_modules/.pnpm/cosmiconfig-typescript-loader@4.4.0_@types+node@20.5.1_cosmiconfig@8.3.6_ts-node@10.9.1_typescript@5.2.2/node_modules/cosmiconfig-typescript-loader/dist/cjs/index.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from /project/package.json.

    at createErrRequireEsm (/project/node_modules/.pnpm/ts-node@10.9.1_@types+node@20.8.4_typescript@5.2.2/node_modules/ts-node/dist-raw/node-internal-errors.js:46:15)
    at assertScriptCanLoadAsCJSImpl (/projects/node_modules/.pnpm/ts-node@10.9.1_@types+node@20.8.4_typescript@5.2.2/node_modules/ts-node/dist-raw/node-internal-modules-cjs-loader.js:584:11)
    at Object.require.extensions.<computed> [as .ts] (/project/node_modules/.pnpm/ts-node@10.9.1_@types+node@20.8.4_typescript@5.2.2/node_modules/ts-node/src/index.ts:1610:5)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Function.Module._load (node:internal/modules/cjs/loader:960:12)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:119:18)
    at /projects/node_modules/.pnpm/cosmiconfig-typescript-loader@4.4.0_@types+node@20.5.1_cosmiconfig@8.3.6_ts-node@10.9.1_typescript@5.2.2/node_modules/cosmiconfig-typescript-loader/dist/cjs/index.js:59:22
    at Explorer.#loadConfiguration (/project/node_modules/.pnpm/cosmiconfig@8.3.6_typescript@5.2.2/node_modules/cosmiconfig/src/Explorer.ts:113:22)
    at Explorer.#readConfiguration (/projects/node_modules/.pnpm/cosmiconfig@8.3.6_typescript@5.2.2/node_modules/cosmiconfig/src/Explorer.ts:85:36) {
  name: '_TypeScriptCompileError',
  filepath: '/project/commitlint.config.ts'
}

Though I tried .js file with no types in ESM format and it worked just fine. By looking at previous error I cannot say that this is error caused by commitlint. Now I am not sure what is the state of this issue :thinking:


The issue persists, not sure why this got closed. Adding support for .cjs is a different concern and not a solution to what this issue describes.

@DamianGlowala can you confirm that now commitlint .js config with ESM "just works"?


Maybe it's issue with cosmic config or ts loader itself? From where this difference between .ts and .js occurs? I wish I had some time to experiment with cosmiconfig.

UPD: Oh.. this is probably related to already mentioned https://github.com/TypeStrong/ts-node/issues/1007 issue. So.. This can be closed now? @escapedcat what do you think?

escapedcat commented 11 months ago

@ZerdoX-x thanks for digging and confirming. If there's nothing we can do on our side we could close this again.

ZerdoX-x commented 10 months ago

I think yes, this can be closed, no response from other guys within a week. I am sure it's blocked by ts-node issue mentioned above

@escapedcat

alextnetto commented 10 months ago

@ZerdoX-x For .js you can use this on the config file.

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

For me, it worked fine after that.

ZerdoX-x commented 10 months ago

@Alextnetto yep, I know. but originally the issue about .ts file.

Personally using

// @ts-check

/* TODO: migrate from .js to .ts when
 * https://github.com/TypeStrong/ts-node/issues/1007
 * gets resolved.
 */

/** @typedef { import("@commitlint/types").UserConfig } */
export default {
    extends: ["@commitlint/config-conventional"],
};

.ts issue still exists but commitlint and cosmiconfig just require ts-node issue mentioned above to be resolved

newives commented 10 months ago

may be related to this https://github.com/cosmiconfig/cosmiconfig/issues/312

pauliesnug commented 10 months ago

may be related to this cosmiconfig/cosmiconfig#312

@newives yes, i got this same error and the issue is cosmiconfig since it require()s the config, the craetor says it will be fixed so commitlint needs to update it once its fixed.

pauliesnug commented 10 months ago

for now, we can just set a package resolution override to "cosmiconfig": "9.0.0-alpha.3"

newives commented 10 months ago

https://github.com/stylelint/stylelint/pull/7329 seems to have fixed the problem. I don’t know if it helps.

joberstein commented 10 months ago

Is this still an error with commitlint@18.2.0? ts-node was removed as a dependency with: https://github.com/conventional-changelog/commitlint/compare/v18.1.0...v18.2.0

newives commented 10 months ago

It is estimated that https://github.com/conventional-changelog/commitlint/pull/3776 has been fixed. If possible, publish a version so I can test it.

escapedcat commented 10 months ago

https://github.com/conventional-changelog/commitlint/releases/tag/v18.4.2

newives commented 10 months ago

@joberstein Very nicely done❤️ I tested commitlint.config files ending with .js and .ts and using the esm syntax export default {} and they all worked fine

pauliesnug commented 10 months ago

Thank you very much!

masabbasi commented 8 months ago

I changed module.exports = to export default and it worked for me.

Thankful. It worked for me.

rolznz commented 2 weeks ago

Updating commitlint cli fixed this for me, also hi @escapedcat ! :grin: