Husky pre-commit hook not ignoring node_modules

2.2k views Asked by At

I recently updated the dependencies on my React Native project, and we have husky pre-coomit hooks set up. I tried committing now and I get the following error:

myname@MacSelf014 my-project % git commit --amend
file:///<path-to-project>/node_modules/listr2/dist/index.js:206
    this.options.fields ??= {};
                        ^^^

SyntaxError: Unexpected token '??='
    at Loader.moduleStrategy (internal/modules/esm/translators.js:149:18)
husky - pre-commit hook exited with code 1 (error)

The pre-commit set up looks like this in my package.json:

"lint-staged": {
    "*.{js,ts,tsx}": [
      "eslint --fix",
      "prettier --write",
    ]

I've tried adding node_modules to .husky/_/.gitignore like so:

*
node_modules/*

And the problem persists. Any idea what the issue might be?

3

There are 3 answers

1
Valentin On

The problem isn't your linters, it's the listr2 package that husky uses to make nice console messages. It's probably your node version.

Check your node version in the terminal you used to make your commit with node -v and make sure yout have at least v16.

Sources :

0
IggyBar On

i was having this issue while trying to commit directly from Rider or from Github client, but when i ran the commands from the terminal, git add . git commit -m '' it worked, so i would suggest you try that!

0
Vencovsky On

As explained in the lint-staged README

Version v13.3.0 was incorrectly released including code of version v14.0.0. This means the breaking changes of v14 are also included in v13.3.0, the last v13 version released

v13.3.0 contains breaking changes of v14 and NodeJs v14 is not supported anymore. The error you see is related to this.

To fix the issue, you can either install a exact version of lint-staged that is prior to the issue

npm i --save-exact [email protected] 

Or upgrade you NodeJs to v14.


Other Source: https://github.com/lint-staged/lint-staged/issues/1315