commitlint: TypeError: format is not a function

Expected Behavior

I’m trying to commit in a new repository that uses husky and my commit should work and pass husky checks as expected.

Current Behavior

When I try to commit, I see the following:

husky > commit-msg (node v10.3.0)
/Users/mprzybylski/Work/Livongo/fe-utils/node_modules/@commitlint/cli/lib/cli.js:113
	throw err;
	^

TypeError: format is not a function
    at /Users/mprzybylski/Work/Livongo/fe-utils/node_modules/@commitlint/cli/lib/cli.js:193:18
    at run (/Users/mprzybylski/Work/Livongo/fe-utils/node_modules/babel-polyfill/node_modules/core-js/modules/es6.promise.js:75:22)
    at /Users/mprzybylski/Work/Livongo/fe-utils/node_modules/babel-polyfill/node_modules/core-js/modules/es6.promise.js:92:30
    at flush (/Users/mprzybylski/Work/Livongo/fe-utils/node_modules/babel-polyfill/node_modules/core-js/modules/_microtask.js:18:9)
    at process._tickCallback (internal/process/next_tick.js:61:11)
husky > commit-msg hook failed (add --no-verify to bypass)

Affected packages

  • cli
  • core
  • prompt
  • config-angular

Possible Solution

I’m not sure, and the odd thing is this works in my pre-existing repos, but it fails in this new one.

Steps to Reproduce (for bugs)

  1. Make changes to files.
  2. Commit changes. Error.

I’ve created a small repo that reproduces the problem here: https://github.com/reintroducing/commit-test

commitlint.config.js ```js module.exports = { extends: ['@spothero/commitlint-config'] }; ```

Context

I can’t commit to the new repo unless i bypass husky checks. I’m using a custom commitlint config which can be found here: https://github.com/spothero/commitlint-config

Your Environment

Executable Version
commitlint --version 7.5.2
git --version 2.20.1 (Apple Git-117)
node --version 10.3.0

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 3
  • Comments: 36 (10 by maintainers)

Commits related to this issue

Most upvoted comments

@robweiss22 @JounQin reinstalling node_modules did it for me. I tried with many different commits, with different options, but after deleting node_modules and installing them again (@commitlint/cli v19.0.3), everything worked ok, and I wasn’t able to reproduce the issue. For what it’s worth, this error happened to me only when using pnpm instead of npm.

removing node_module with pnpm did help

Hi! Got this error on 19.0.3 (Node 21.2.0). It’s on @commitlint/cli:

(...)/node_modules/@commitlint/cli/lib/cli.js:129
        throw err;
        ^

TypeError: format is not a function
    at main (file://(...)/node_modules/.pnpm/@commitlint+cli@19.0.3_@types+node@20.11.24_typescript@5.3.3/node_modules/@commitlint/cli/lib/cli.js:280:20)

I’m importing from @commitlint/format and I have the same problem.

The docs for @commitlint/format are no longer correct.

Test code for @commitlint/format:

const format = require('@commitlint/format')
console.log('format', typeof format)
format({})

Result: 7.5.0

format function

Result: 7.6.0

format object
// { default: [Function: format],
//  formatResult: [Function: formatResult] }
[ERROR] TypeError: format is not a function

Test code for @commitlint/core:

const { read, load, lint, format } = require('@commitlint/core')

console.log('format', typeof format)
console.log('load', typeof load)
console.log('read', typeof read)
console.log('lint', typeof lint)

Result: 7.5.0

format function
load function
read function
lint function

Result: 7.6.0

format object
load function
read function
lint function
[ERROR] TypeError: format is not a function

To me this is actually a breaking change for @commitlint/format and @commitlint/core since the API has changed in a non-backwards compatible way.

Is this intentional and going forward we should import from require('@commitlint/format').default?

I ran into this today, and adding:

public-hoist-pattern[]=@commitlint*
public-hoist-pattern[]=commitlint

… to .npmrc, then running pnpm install, fixed it for me, with pnpm@9.0.5.

give it a push, I’m getting this in 19.0.3

@elliotleelewis thanks for the info. Someone just mentioned that the babel preset ist still targeting node v6. Might be good to clean this up. Instead of targeting es5 maybe we should just target node 12 because that’s the currently supported node version.

this is unrelated, as babel is not used to compile code anymore

The only difference that I can see between them is the async tag on the method declaration in @commitlint/lint? Using target: “es5” should fix that? 😃

in theory yes, but this is really bad idea, as code is going to be way slower (due to transforms)


personally i will recommend adding non default export to load like it has been done to format, and let ppl import as

const { load } = require('@commitlint/load')

@elliotleelewis thanks for the info.
Someone just mentioned that the babel preset ist still targeting node v6.
Might be good to clean this up. Instead of targeting es5 maybe we should just target node 12 because that’s the currently supported node version.

I think the problem is with your tsconfig.shared.json. Your target is set to es2017 instead of es5, so the default method being exported is an async method, and I’m not sure if the require(...) syntax stumbles over that?

Any particular reason why we’re targeting es2017 vs es5?

EDIT: Here’s what I see when inspecting a couple of packages’ contents…

@commitlint/resolve-extends works perfectly fine, and here is its default export (lines 23 and 35):

image

@commitlint/lint doesn’t work, and here is its default export (lines 12 and 117):

image

The only difference that I can see between them is the async tag on the method declaration in @commitlint/lint? Using target: "es5" should fix that? 😃

Yep, 9.1.1 @commitlint/lint and @commitlint/load are broken.

const load = require('@commitlint/load')
console.log(load)
/* =>

{ default: [AsyncFunction: load] } 
*/

The fix is just add .default: const load = require('@commitlint/load').default. The API docs need to be updated if this was intended.

This is also broken in 9.1.1 with @commitlint/lint. 😢

Get the same error: TypeError: lint is not a function

Hi @marionebl

This is broken again in 8.0.0 with no mention in the changelog.

const format = require('@commitlint/format')
console.log(format)
/* result =>

{ __esModule: true,
  default: [Function: format],
  format: [Function: format],
  formatResult: [Function: formatResult] }

*/

The imports for read, load, lint all have the default export exposed. Is the plan to change those as well?

Either way, I didn’t see the breaking change for format in the changelog so I am unsure if this is intentional.

This change solves it, but is is inconsistent with the sibling packages:

@@ -1,7 +1,7 @@
 const read = require('@commitlint/read')
 const load = require('@commitlint/load')
 const lint = require('@commitlint/lint')
-const format = require('@commitlint/format')
+const { format } = require('@commitlint/format')
 const config = require('@commitlint/config-conventional')

Fixed via @commitlint/format@7.6.1