问题
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