npm ci runs postinstall script


(Hecht Software) #1

What I Wanted to Do

I defined a postinstall script in my root package.json. I run npm ci and didn’t expect the postinstall script to be executed by npm.

What Happened Instead

The postinstall script was executed, altough I did not run npm install.

Reproduction Steps

add script in your package.json:

"scripts": {"postinstall": "echo test"}

run ci script:

npm ci

Details

Maybe the npm devs thought, npm ci is also an installation process, so it makes sense to run preinstall and postinstall. This is absolutely necessary for dependecies but it is a problem for pre-/post-scripts in the root package.json.

Why? In my case I’m using postinstall to install dependencies in a sub-module:

"scripts": {"postinstall": "cd my-child-module && npm install"}

For my CI environment, I want to have more control over child module initialization. A first requirement is that the child module must be installed with npm ci if the root package is installed with npm ci:

"scripts": {
    "postinstall": "cd my-child-module && npm install",
    "postci": "cd my-child-module && npm ci"
}

As a next step I’m going to add a caching mechanism which skips npm ci if package-lock.json hasn’t been changed. Of course I want to keep the install logic for my local development environment. For such requirements I have to separate logic for my post-scripts.

Platform Info

$ npm --versions
    { 'de.hechtsoftware.hwkprototype': '1.0.0',
      npm: '6.4.1',
      ares: '1.10.1-DEV',
      cldr: '32.0',
      http_parser: '2.8.0',
      icu: '60.1',
      modules: '57',
      napi: '3',
      nghttp2: '1.32.0',
      node: '8.12.0',
      openssl: '1.0.2p',
      tz: '2017c',
      unicode: '10.0',
      uv: '1.19.2',
      v8: '6.2.414.66',
      zlib: '1.2.11' }
$ node -p process.platform
linux