我希望有一个库或工具可以运行我的代码并告诉我运行它需要什么版本的Node。也许更好的是它会提醒我代码区域可以更改以支持旧版本。
野外有什么类似的吗?
答案 0 :(得分:0)
我不确定这究竟是您要找的,但是there is an existing package.json
property called "engines"包开发人员可以指定他们需要的版本。使用glob
和semver
包来查看所有带有"引擎"的package.json文件并不困难。要求并将其编译成以下对象:
{
[version1]: [{ packageName, currentlySupported }, { ... }],
[version2]: [...],
...
}
以下是为您创建该对象的脚本的基本示例:
npm install glob semver
checkversions.js:
const glob = require('glob');
const path = require('path');
const semver = require('semver');
const currentVersion = process.version;
const versions = {};
glob('node_modules/*/package.json', (err, files) => {
files.forEach((file) => {
const pkg = require(path.resolve(__dirname, file));
// only check add package if it specifies "engines"
if (pkg.engines && pkg.engines.node) {
const reqdVersion = pkg.engines.node.replace(/\s+/g, '');
// assume you are using a supported version
let currentlySupported = true;
// check if current node version satisfies package requirements
if (!semver.satisfies(currentVersion, reqdVersion)) {
currentlySupported = false;
}
if (!Array.isArray(versions[reqdVersion])) {
versions[reqdVersion] = [];
}
versions[reqdVersion].push({
package: file.replace(/node_modules\/(.*)\/package.json/, '$1'),
currentlySupported,
});
}
});
console.log(versions);
});
运行它:
node checkversions.js