Install subset of dependencies with npm

房东的猫 提交于 2020-01-15 06:22:28

问题


Is there a way to specify subsets of dependencies in npm, with an alias or "feature tag"? That is, if someone knows that they will only be using some limited subset of the features of my package, they can specify those features and, on npm install, only download the dependencies relevant to those features?

My package has a very large number of dependencies and takes nearly half an hour to install, but most users only need a subset of their functionality. I'm thinking of something like how dependencies can be divided into devDependencies and dependencies, but with n groups instead of just those two. For example:

npm install --feature feature1 --feature feature2

From reading the docs, I think the answer here is "no", but what would be your suggestion for this case? Split the package into smaller plugin packages and have users install the plugins that they want? I don't want something that is too complicated for users to configure.


回答1:


The short answer is no, NPM was not designed for this mostly because dependency trees are incredibly large, and this use case could really just complicate things for most users. That being said, I wrote a package to do it.

My package is install-subset, and can be installed globally with npm install -g install-subset

https://www.npmjs.com/package/install-subset

Essentially you build inclusion lists and exclusion lists for named subsets in your package.json like this:

"subsets": {
    "build": {
        "include": [
            "babel-cli",
            "dotenv"
        ]
    },
    "test": {
        "exclude": [
            "eslint",
            "lint-rules",
            "prettier"
        ]
    }
}

Then call it with, for example, install-subset test

This will temporarily rewrite your package.json to not install those packages excluded, then restore it, which depending on the packages can save a lot of time and bandwidth.

Also works with yarn, is open source and issues/PRs are welcome.




回答2:


Another third-party package that you can use to do this is group-dependencies. It allows you to define [GROUP_NAME]Dependencies arrays (note: not objects) in your package file, then install just that subset using deps install [GROUP_NAME].

Here's an example from their README:

{
  ...  
  "devDependencies": {
    "intercept-stdout": "^0.1.2",
    "jest": "^20.0.4",
    "strip-color": "^0.1.0"
  },
  // our new group representing testing dependencies
  "testDependencies": [
    "jest"
  ]
  ...
}

Now you can install only the dependencies for this new group:

# This will install jest@^20.0.4:  
$(npm bin)/deps install test 


来源:https://stackoverflow.com/questions/40886871/install-subset-of-dependencies-with-npm

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