2

I cannot seem to get husky and lint-staged to work on my Windows 10 machine.

Currently my setup is as follows:

.huskyrc.json

{
  "hooks": {
    "pre-commit": "lint-staged"
  }
}

.lintstagedrc (though it seems not to matter whats in here since the problem appears to occur before this file is even read)

{
  "**/*.+(js|md)": [
    "prettier --write",
    "eslint --fix src/",
    "git add"
  ]
}

package.json

  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --watchAll=false",
    "eject": "react-scripts eject",
    "lint": "eslint .",
    "lint-fix": "eslint . --fix"
  },
  ...
  "devDependencies": {
    ...
    "husky": "^4.3.0",
    "lint-staged": "^10.5.4",
    ...
  }

I am using npm version 6.14.11 and node version 14.15.1.

Now, when I enter git add . and git commit -m "test" in the console this is the result:

husky > pre-commit (node v14.15.1)
C:\Program Files\nodejs/node_modules/node/bin/node: line 1: This: command not found
husky > pre-commit hook failed (add --no-verify to bypass)

This appears to only happen on my Windows machine and does not seem to be a problem on Ubuntu. What could be going on here?

Raul Špilev
  • 288
  • 4
  • 18

1 Answers1

1

If you installed lint-staged locally, then you need to prepend the path to lint-staged.

.huskyrc.json

{
  "hooks": {
    "pre-commit": "./node_modules/.bin/lint-staged"
  }
}

Another way is to add a script to your package.json that runs lint-staged.

package.json

{
  "scripts": {
    "lint-staged": "lint-staged"
  }
}

Then modify your pre-commit hook.

.huskyrc.json

{
  "hooks": {
    "pre-commit": "npm lint-staged"
  }
}

Of course, you could also choose to install lint-staged globally, in which case, you shouldn’t need to make any changes. Just run: npm install -g lint-staged

Frederik Krautwald
  • 1,782
  • 23
  • 32