5

我已经部署了 Node.js 的 0.6 版本,其中为各种项目安装了大量的包。

有没有一种直接的方法来检查所有使用 NPM 安装的包,看看它们是否支持 Node.js v 0.8.x?

我可以看到 package.json 文件应该说明它们的 Node 版本,尽管我猜很多人不会包含这个 - 所以我真的只对那些说它们肯定与 Node v 0.8兼容的包感兴趣。X

例如,他们在 package.json 中有这样的内容:

"engines": {
  "node": "<0.8.0"
},

或者

"engines": {
  "node": "=0.6.*"
},

我只想要一个不兼容的软件包的简单列表。

4

2 回答 2

4

在应用程序的基本目录中尝试此操作:

find . -name package.json -exec node -e 'var e = JSON.parse(require("fs").readFileSync(process.argv[1]))["engines"]; if (e && e.node) { var bad = false; if (e.node.match(/<\s*0\.[0-8]([^.]|\.0)/)) bad = true; if (e.node.match(/(^|[^>])=\s*0\.[^8]/)) bad = true; if (bad) console.log(process.argv[1], "appears no good (", e.node, ")") }' '{}' \;

翻译成普通风格:

var fs = require("fs");

var contents = fs.readFileSync(process.argv[1]);
var package = JSON.parse(contents);
var engines = package.engines;

if (engines && engines.node) {
    var node = engines.node,
        bad = false;

    if (node.match(/<\s*0\.[0-8]([^.]|\.0)/)) {
        // Looks like "< 0.8.0" or "< 0.8" (but not "< 0.8.1").
        bad = true;
    }

    if (node.match(/(^|[^>])=\s*0\.[^8]/)) {
        // Looks like "= 0.7" or "= 0.9" (but not ">= 0.6").
        bad = true;
    }

    if (bad) {
        console.log(process.argv[1], "appears no good (", node, ")");
    }
}

然后,我们使用在我们能找到find的每一个上运行它。package.json

这是我在express-template.coffee包上运行它时得到的结果:

./node_modules/jade/node_modules/commander/package.json appears no good ( >= 0.4.x < 0.8.0 )
./node_modules/mocha/node_modules/commander/package.json appears no good ( >= 0.4.x < 0.8.0 )
./node_modules/mocha/package.json appears no good ( >= 0.4.x < 0.8.0 )

似乎TJ反对 0.8 ;-)

于 2012-06-27T13:20:50.690 回答
1

npm view <packageName> engines

有关更多信息,请参阅npm 查看 文档

例如:

npm view jest verson返回最新版本,在我的情况下 27.1.0 npm view jest engines给你:

{ node: '^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0' }

其中告诉你最新版本的 jest v27 支持节点 10.13、12.13、14.15 和 15。

npm view jest@25 engines但是,告诉您它支持节点 8.3 上的任何内容:

jest@25.5.2 { node: '>= 8.3' }
jest@25.5.3 { node: '>= 8.3' }
jest@25.5.4 { node: '>= 8.3' }

(这个回答是对一个老问题,但它仍然出现在我的谷歌搜索中)

于 2021-09-02T04:08:08.400 回答