18

I am trying to make a test for npm packages for my project such that every time I try to install a module i.e run npm install <module> a script must be run before that module is installed. The preinstall script only works with npm install and not with npm install <module>.

eg :- If run npm install request . It should run a script which shows me all the dependencies of the request module before installing the module. Thanks in advance.

yawningphantom
  • 386
  • 1
  • 3
  • 9
  • Are you asking how to do when when (a) Your module is installed (b) Any module is installed when npm install is run in the same directory as your package.json or (c) Any module is installed system wide on your computer? – Quentin Oct 13 '17 at 08:23
  • (b) any module is installed when `npm install ` is run in the same directory as package.json . just to make myself clear -> for installing an package / module you run the command `npm install name_of_module` . I want a script to be run every time i try to install any package / module from npm . – yawningphantom Oct 13 '17 at 11:54
  • You could run `$ npm view dependencies`, (to list the dependencies), before running `$ npm install ` as mentioned in the [docs](https://docs.npmjs.com/cli/view). Or must `$ npm view dependencies` be automatically invoked every time you run `$ npm view `? – RobC Oct 13 '17 at 13:26
  • yeah it can be done by using npm view and shell scripting but I was hoping to get a better solution so that my team can follow the same convention anyway I create a shell script and added the script node . now it asks for the name of the module , show dependencies and version and then ask whether I would like to install the package. – yawningphantom Oct 13 '17 at 16:22
  • In which case you’ve almost answered your question here over [here](https://stackoverflow.com/questions/46556921/npm-preinstall-script) – RobC Oct 13 '17 at 16:52
  • Yeah :-( , But I was hoping for something better . anyways thanks for the help – yawningphantom Oct 14 '17 at 07:56
  • If installing from git is an option for you (instead of npm), you could use the prepare hook. – ford04 May 13 '19 at 21:46
  • Does this answer your question? [How to run a post-install script after individual execution of "npm install "](https://stackoverflow.com/questions/44362462/how-to-run-a-post-install-script-after-individual-execution-of-npm-install-pac) – Venryx Mar 26 '21 at 16:39

1 Answers1

7

Add "install": "[Your Command]" in the script part of your package.json

Example:

{
    "name": "test",
    "version": "1.0.0",
    "description": "A sample test",
    "main": "index.js",
    "scripts": {
        "install": "echo Test"
    }
}

You can also use a pre hook with "preinstall": "[Your Command]"

DarkyZ
  • 349
  • 2
  • 11
  • 14
    I think i might not have framed my question correctly . for installing a package / module you run the command `npm install name_of_module` . I want a script to be run every time i try to install any package / module from npm . the preinstall and postinstall work only with the `npm install` command and not with the `npm install ` command . – yawningphantom Oct 13 '17 at 11:52
  • True. Did you get anything @yawningphantom? – HalfWebDev Jan 28 '19 at 07:49
  • Also looking forward this for npm. Tried that `yarn` will execute 'preinstall' script before installing any dependency. – merlin.ye Nov 14 '19 at 13:19
  • The answer works well for a specific package. But the question is for any package that you install with npm, the pre-install script needs to run. @yawningphantom do you have a solution yet? Please share how you solved this scenario. I am struggling with the same situation. – Siva Senthil Jan 16 '20 at 08:19
  • 1
    @yarningphantom Here is another SO page for your question: https://stackoverflow.com/questions/44362462 There is one solution listed there (npm hook scripts in the `/node_modules/.hooks` folder), but unfortunately it doesn't work on Windows as far as I can tell. :( – Venryx Mar 26 '21 at 16:40