Search code examples
npmnpm-installnpm-scripts

Do dependencies get downloaded before or after the npm preinstall script?


The docs for NPM Scripts state:

preinstall: Run BEFORE the package is installed

But does this occur before or after dependencies are downloaded?

That is, which of these is correct:

  1. Download Dependencies
  2. Pre-install script run
  3. Installation

or

  1. Pre-install script run
  2. Install
    1. Download Dependencies as part of the installation

I couldn't see this covered by the npm script docs, but it's possible it's documented elsewhere.


Solution

  • I have also searched for the answer to this question and haven't found any documentation outlining the exact process.

    However, from testing, it seems to depend. If you run npm link or npm install within a package directory, it will download dependencies and run scripts, but in this order:

    1. Run preinstall script
    2. Download dependencies
    3. Run postinstall script

    Therefore, if your preinstall script uses dependencies, it will fail.

    But if you run npm install <package> or npm install <path/to/local/folder> from another folder, it will do it in this order:

    1. Download or link package + all dependencies
    2. Run preinstall script
    3. Run postinstall script