node.js - 获取所有包含某个关键字的npm包

标签 node.js npm keyword

我想获得所有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/

相关文章:

html - Node JS 配置

node.js - Mongoose 双向模型/多对多关系

javascript - 无法在 Node 中运行新的 firebase

javascript - d3 v4 中的二维画笔

search - 知道哪些用户在 Google 上搜索到我的网站

node.js - Typescript Express 请求输入

javascript - 创建登录时,到达一个空对象

node.js - 无法将 Angular 更新到版本 6

python - 获取实际传递给 Python 方法的关键字参数

mysql - SELECT 后的保留关键字