最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to run a script before installing any npm module? - Stack Overflow

programmeradmin6浏览0评论

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.

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.

Share Improve this question edited Oct 13, 2017 at 12:04 yawningphantom asked Oct 13, 2017 at 8:09 yawningphantomyawningphantom 4461 gold badge4 silver badges9 bronze badges 8
  • 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 puter? – Quentin Commented Oct 13, 2017 at 8:23
  • (b) any module is installed when npm install <module> is run in the same directory as package.json . just to make myself clear -> for installing an package / module you run the mand npm install name_of_module . I want a script to be run every time i try to install any package / module from npm . – yawningphantom Commented Oct 13, 2017 at 11:54
  • 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>? – RobC Commented Oct 13, 2017 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 Commented Oct 13, 2017 at 16:22
  • In which case you’ve almost answered your question here over here – RobC Commented Oct 13, 2017 at 16:52
 |  Show 3 more ments

1 Answer 1

Reset to default 10

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

发布评论

评论列表(0)

  1. 暂无评论