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.

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

See original GitHub issue
// 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

  • cli
  • core
  • prompt
  • config-angular

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

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:8
  • Comments:5

github_iconTop GitHub Comments

9reactions
jjbell150commented, Aug 19, 2022

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

0reactions
ZerdoX-xcommented, Dec 26, 2022

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…

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript and native ESM on Node.js - 2ality
In this blog post, I'll explain everything you need to know in order to use and produce native ECMAScript modules on Node.js.
Read more >
Error [ERR_REQUIRE_ESM] - Husky, lint-staged, eslint
The problem is, the project is in it's first steps, quickly redoing the project in another machine on the same node and yarn,...
Read more >
ts-node error [err_require_esm]: require() of es module
ts Error: Jest: Failed to parse the TypeScript config file C:\Projects\appAngularfrontend\kendoUpdate\jes t.config.ts Error [ERR_REQUIRE_ESM]: require() of ES ...
Read more >
Using ES Modules (ESM) in Node.js: A Practical Guide (Part 3)
Let's start exploring the package, tool by tool. We'll start with the most important of them, ESLint. ESlint configuration for Node.js ESM #....
Read more >
Error ERR REQUIRE ESM | Must use import to load ES Module
How to use ES Modules in Node. · Top 5 Excel Functions for Finance People (with end-to-end example) · Require vs Import Javascript...
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