Lawrence Wagerfield
Lawrence Wagerfield

Reputation: 6611

NPM: How to add a "post-install" hook that's not run for package consumers?

I'm writing an NPM module.

I'd like to automate some tasks after every npm install when developing the module locally.

However, I do not want these steps to be performed when consumers of my library perform an npm install and I do not want these steps to be performed after every npm pack and npm publish that I make during development (so this rules-out using the prepublish and prepare scripts).

What's the easiest way to achieve this?

(I've considered: (a) publish a separate package.json w/o the install script, (b) create a ./install.sh in the project's root that users call instead of npm install... but this kinda sucks.)

Upvotes: 3

Views: 1314

Answers (1)

Lawrence Wagerfield
Lawrence Wagerfield

Reputation: 6611

The install:local script here will run after npm install is run locally only (i.e. not when consumers install your package).

package.json:

{
  ...,
  "scripts": {
    "prepare": "case \"$npm_config_argv\" in *\"\\\"install\\\"\"*|*\"\\\"ci\\\"\"*) npm run install:local ;; esac",
    "install:local": "echo 'npm install' was run directly in the project, and not by a library consumer!",
  }
}

Upvotes: 2

Related Questions