commitlint
commitlint copied to clipboard
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
- [X] 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)
- Make changes to files.
- 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 |
I am seeing the same thing. It only appeared after upgrading to [email protected].
for what its worth, my commitlint setup is pinned to [email protected].
I went ahead and added a link to a repo I just created which reproduces the problem: https://github.com/reintroducing/commit-test
Also seeing it, started today after package updates!
Using VSCode with prettier and eslint
Thanks for reporting, I'll look into this.
Could reproduce - this is caused by a mismatch between the @commitlint/format
(7.6.0) and @commitlint/cli
(7.5.1) package versions. Could you install the latest cli and check if this fixes the issue for you?
npm install @commitlint/cli@latest
@marionebl @commitlint/core
exported the wrong format
?
@marionebl worked for me, thanks!!
@marionebl that did indeed fix it, thank you, appreciate the quick turn around on this.
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
?
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.
Thanks for the analysis - you are right about the accidental breaking change. I'm in the process of creating a patch release that addresses this.
Is this intentional and going forward we should import from require('@commitlint/format').default?
No, when 7.6.1
and (later) 8.0.0
are released you'll be able to require the default
export of @commitlint/format
directly.
@marionebl thanks for your response, and effort in fixing it. 👍
Fixed via @commitlint/[email protected]
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')
I'm currently working around this in a (hopefully) forward-compatible way like:
import * as commitlint from '@commitlint/core';
import interopRequireDefault from '@babel/runtime/helpers/interopRequireDefault';
const format = interopRequireDefault(commitlint.format),
lint = interopRequireDefault(commitlint.lint),
load = interopRequireDefault(commitlint.load);
// format is not a function
Since this is an error again (per @varl) and as recent as 8.3.2
, can we re-open this issue?
This is also broken in 9.1.1
with @commitlint/lint
. 😢
Get the same error: TypeError: lint is not a function
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.
@salolivares thanks for the info!
Does this solve the issue?
If yes, would you mind updating the docs?
@escapedcat I dont think that's a long-term solution. This seems like a workaround?
Not sure what to do with this.
If .default
works I feel it's good solution.
Or anyone has a suggestion what might need to be changed here to create a better solution?
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):

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

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? 😃
@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.
@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 targetnode 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')
give it a push, I'm getting this in 19.0.3
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/@[email protected]_@[email protected][email protected]/node_modules/@commitlint/cli/lib/cli.js:280:20)
Also seeing this error as of a today:
node_modules/@commitlint/cli/lib/cli.js:129
throw err;
^
TypeError: format is not a function
@ezeamin @robweiss22 Please provide online reproduction instead of only error messages.
@JounQin Please see this example: https://github.com/Vunovati/pino-opentelemetry-transport/actions/runs/8142947695/job/22339436738?pr=147#step:7:9
@robweiss22 I can't reproduce it on GitPod, you need to provide an online and runnable reproduction with steps.
Our issue is from a private repo, so I can't share our exact case. For now we have downgraded to version 18 which worked for us. Hopefully @ezeamin can share some more information.