Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Husky pre-commit hook not ignoring node_modules

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?

like image 958
Rameez Hussain Avatar asked Oct 12 '25 16:10

Rameez Hussain


2 Answers

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 :

  • https://www.npmjs.com/package/listr2
  • https://github.com/listr2/listr2/issues/689
like image 83
Valentin Avatar answered Oct 14 '25 05:10

Valentin


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

like image 28
Vencovsky Avatar answered Oct 14 '25 05:10

Vencovsky