Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm pre commit not working

I am using npm precommit hook, but it is not stopping a file with issues to be committed, nor am I getting the message "Pre commit checks" when I try to commit a file.

Package Json:
{
  "name": "myfolder",
  "version": "1.0.0",
  "description": "",
  "main": "",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 0",
    "precommit-msg": "echo 'Pre-commit checks...' && exit 0",
    "lint": "csslint global/css"
  },
  "author": "SR",
  "license": "ISC",
  "dependencies": {
    "csslint": "^1.0.4",
    "jshint": "^2.9.4",
    "pre-commit": "^1.2.2"
  },
  "pre-commit": [
    "precommit-msg",
    "lint"
  ],
  "devDependencies": {
    "pre-commit": "^1.2.2"
  }
}
like image 881
user7258044 Avatar asked Mar 17 '17 18:03

user7258044


People also ask

How to create a pre-commit in Git?

Meaning, simple shell scripts will work right off the bat. For example, the Git hook pre-commit that ships with an initialization of a Git repository can be run without any modification. Navigate to the Git hooks directory with the command cd .git\hooks This directory holds all the Git hook scripts. Create a file named pre-commit. Note:

Do I need unstash and stash for npm test?

The way npm test is configured, in the same way, any other tasks can be executed. And of course for other tasks stash and unstash won't be compulsory. Here is the GitHub repo with an example. Containers Vs Virtual Machines, which is better and why? If you have reached here, then I did a satisfactory effort to keep you reading.

Why does my pre-commit Git hook fail on Windows?

For example, when using the Husky Git hook plugin for NPM, along with the Prettier formatter, the commands do not map 1-1. Meaning that your pre-commit Git hook will fail on Windows. A Git hook is an executable script; however, you are using a command prompt script ( .cmd) and not a shell script ( .sh ).


2 Answers

Please, make sure that your 'package.json' file is in the same folder, where '.git' folder is (where git repository was initialized). When you install 'pre-commit' package, 'pre-commit' file should appear under '.git/hooks/'.

enter image description here

like image 144
pasul Avatar answered Sep 28 '22 09:09

pasul


Just FYI I had this issue because the pre-commit file was missing in the hooks folder.

Running npm i pre-commit --save-dev again created the file and solved it for me.

like image 20
Lukas Oppermann Avatar answered Sep 28 '22 09:09

Lukas Oppermann