Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a script before installing any npm module?

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.

like image 350
yawningphantom Avatar asked Oct 13 '17 08:10

yawningphantom


People also ask

What should I run before npm install?

You could run $ npm view <package_name> dependencies , (to list the dependencies), before running $ npm install <package_name> as mentioned in the docs. Or must $ npm view <package_name> dependencies be automatically invoked every time you run $ npm view <package_name> ?

How do I run a npm script?

To execute your Script, use the 'npm run <NAME-OF-YOUR-SCRIPT>' command. Some predefined aliases convert to npm run, like npm test or npm start, you can use them interchangeably. Maintaining commands you run regularly as an npm script is common, like starting your application, linting, building production apps, etc.

Does npm install run scripts?

npm run install only runs the package. json scripts. install , it will not install dependencies.

What is npm install script?

An npm script is a convenient way to bundle common shell commands for your project. They are typically commands, or a string of commands, which would normally be entered at the command line in order to do something with your application. Scripts are stored in a project's package.


1 Answers

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]"

like image 179
DarkyZ Avatar answered Sep 18 '22 15:09

DarkyZ