npm install: Use global package if it exists, rather than installing twice

∥☆過路亽.° 提交于 2019-12-05 20:26:56

问题


When using npm install with a package.json file, how do I get it to use a globally installed package that meets the criteria instead of downloading and installing the package locally again?

I know about link, but is there a way to do what I'm describing?


回答1:


One way of doing it for a specific set of modules is removing these modules from the dependencies section and creating a prestart script that contains all the modules that you prefer having installed globally.

A simple example might look something like this:

  "scripts": {
    "test": "mocha",
    "prestart": "npm i -g mocha mysql bluebird"
  },

Instead of prestart you can use one of the other hooks such as preinstall and prepare. Note that this won't work as-is with packages you want to publish and would require a bit more hacking.

Help on running scriipts: https://docs.npmjs.com/misc/scripts




回答2:


Yarn seems to work much better with repeated dependencies. So try yarn install instead of npm install.



来源:https://stackoverflow.com/questions/37370396/npm-install-use-global-package-if-it-exists-rather-than-installing-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!