How to install git hooks on “npm install”?

I’d like to install a pre-commit git hook (that lints the code) when someone installs my-package.

I tried to add a postinstall script:

  • How to get 3-way merge in GIT on non-conflict merges?
  • How to bring the git remote branch references local
  • Git submodule new version update
  • Trying to download an exe file from a website and run it
  • INTEL-XDK and Source/Version control - Is it even supported?
  • git pull results in conflict
  • "scripts": {
      "postinstall": "./scripts/install-git-hooks"

    This works great. When someone runs npm install, they get the pre-commit hook installed.

    However, if another-package depends on my-package, running npm install for another-package runs the postinstall script as well, which is undesired.

    What’s the cleanest way to avoid this undesired affect?

  • How to edit/amend commit messages for commits previous to the last in Git?
  • how to get git short hash in to a variable in Jenkins running on windows 2008
  • Git fetch/checkout without creating remote?
  • Github HTTPS cloning via proxy
  • How can I keep my branch up to date with master with git?
  • Making another Local Branch with Egit
  • 2 Solutions collect form web for “How to install git hooks on “npm install”?”

    You can use the ghooks npm module and add it as a dev-dependency. You can configure what to run before commit in your package.json like so:

    "config": {
        "ghooks": {
            "pre-commit": "npm test"

    Hacky, but might work for you.

    The trick is to identify (within the script) if it is a sub-dependency or a root dependency for the NPM installation. Simply check if ../../package.json exists. If so, it’s a sub dependency and you should skip installing the hooks.

    It should be noted that you are breaking any consistent installation rules, which is exactly against the spirit of the installation scripts. This is to install client-side hooks which cannot be trusted by any means, if you need the linting to be enforced, this should be done server side, where it can just reject code that doesn’t comply.

    Potentially this issue would be better solved like you mentioned, by having it as a custom install script, and just dealing with the additional communication overhead.

    Git Baby is a git and github fan, let's start git clone.