我想获得所有npm包含 foo
关键字的包。
如何使用 NPM api 实现这一点?我找不到任何与此相关的文档。是否可以使用 npm
库来做到这一点?
最佳答案
您可以使用npm
package's search
函数,像这样
var npm = require("npm");
npm.load({}, function () {
npm.commands.search("foo", true, function (err, result) {
if (err) {
throw new Error(err);
}
console.log(Object.keys(result));
});
});
它为 foo
关键字返回 355
个对象。
我们传递给 search
函数的回调函数将接收一个对象,其中键是包名称,值是有关包的信息。引用文档,
Returns an object where each key is the name of a package, and the value is information about that package along with a 'words' property, which is a space-delimited string of all of the interesting words in that package. The only properties included are those that are searched, which generally include:
- name
- description
- maintainers
- url
- keywords
注意:传递给 search
函数的第二个参数 (true
) 是为了防止该函数在 stdout 上打印结果。基本上它会启用静音模式。
您还可以通过使用多个关键字来限制搜索,方法是将数组传递给 search
函数,如下所示
npm.commands.search(["foo", "bar"], true, function (err, result) {
仅返回 39 个对象。
关于node.js - 获取所有包含某个关键字的npm包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29667151/