在用户可以在我的模块上运行他的 npm install
之前,我只想允许特定版本的 Node 和 npm。
在the NPM documentation ,有一个 engine
属性,专用于此任务:
"engines": {
"node": ">=4.0.0",
"npm": ">=3.0.0"
}
这些参数将只允许 Node 和 npm 版本高达 4 和 3。
但是,文档说应该启用 engine-strict
属性以检查版本。在 engine-strict
上,it is said that :
This feature was deprecated with npm 3.0.0
那么,有没有办法使用 npm3 为模块定义最小的 Node 和 NPM 版本?
最佳答案
Yarn 在安装时开箱检查引擎。但是对于 npm,此时您可能必须编写一个自定义验证程序,以便在触发脚本之前检查 Node 版本。
export function verifyNodeVersion(version) {
const nodeVersion = process.versions.node;
if (!nodeVersion.startsWith(`${version}.`)) {
throw new Error(
`Incorrect version of Node found: ${nodeVersion}, Expected: v${version}.x`
);
}
console.log('Current Node version:', nodeVersion);
return true;
}
并在入口点调用函数。
关于javascript - 有没有办法在安装 npm 之前检查 Node 和 npm 版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36396943/