kazazor / git-pre-commit

A pre-commit hook that ignores all the unstagged changes while performing the pre-commit command you wrote (Using Shell, Gulp, Grunt etc..)
https://www.npmjs.com/package/git-pre-commit
MIT License
18 stars 5 forks source link

The pre commit hook throws ENOENT error for scripts with executable that are not installed globally #26

Closed kazazor closed 7 years ago

kazazor commented 7 years ago

The example script:

"precommit": "gulp pre-commit --env=development"

The error:

Error: spawn gulp ENOENT
    at exports._errnoException (util.js:1026:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
    at onErrorNT (internal/child_process.js:359:16)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)
    at Module.runMain (module.js:592:11)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3
kazazor commented 7 years ago

This is being caused because the precommit hook was defined like this:

"precommit": "gulp pre-commit --env=development"

Instead of:

"precommit": "node_modules/.bin/gulp pre-commit --env=development"

The way git-pre-commit currently works is that it takes the script's command and execute it (gulp pre-commit --env=development).

By doing that, we do not gain the PATH resolution in the npm run command, that says:

In addition to the shell's pre-existing PATH, npm run adds node_modules/.bin to the PATH provided to scripts. Any binaries provided by locally-installed dependencies can be used without the node_modules/.bin prefix. For example, if there is a devDependency on tap in your package, you should write:

"scripts": {"test": "tap test/\*.js"}

instead of

"scripts": {"test": "node_modules/.bin/tap test/\*.js"} to run your tests.

So if you do not have gulp installed globally git-pre-commit will cause issues for you.

The solution

Instead of running the command inside the npm script, run the command npm run precommit