21

I have an NPM project, when npm install is run, I'd like to run a custom script.

I tried using this in package.json:

"scripts": {
    "ng": "ng",
    "start": "ng serve",
    "install": "./scripts/install.sh",   // <<<<
 },

but that actually just resulted in an infinite loop. The reason I am looking for this, is because there are tools that simply call npm install, so I can't control what they run. Otherwise, if I had control, I would just call ./scripts/install.sh myself instead.

Note that this is probably not the best idea, just curious if it's possible.

Note my install script looks something like this:

#!/usr/bin/env bash

export FOO="bar";
export PUPPETEER_SKIP_CHROMIUM_DOWNLOAD="true";

npm install
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • 1
    Have you tried "postinstall": "yourScript" ? – BeeBee8 Feb 19 '18 at 03:08
  • Unfortunately modifying postinstall won't work for my use case, since I need to set some env variables for my install script. I think a preinstall script might work, if the env variables for my pre-install script were used by install as well? – Alexander Mills Feb 19 '18 at 03:43
  • 2
    Yes, sorry you need preinstall, yes I think environment variables will be persisted for install as well, give it a try. – BeeBee8 Feb 19 '18 at 04:27

1 Answers1

26

Use preinstall to run code before npm install. Don't try to override npm install in this fashion where you would end up with an infinite loop of calls to npm install.

You can also set environment variables using the config property of package.json. See docs for details

Jay
  • 3,471
  • 4
  • 35
  • 49