[英]Yarn Install - how to force the latest minor version and patch of node (10.x.x)
I have a Node app that is tested on node 10. I am using yarn as a dependency manager.我有一个在节点 10 上测试的 Node 应用程序。我使用 yarn 作为依赖项管理器。 As my app test is run on CI with the latest version of node 10, I want to make sure that all developers have installed the latest 10.xx version when running any yarn command.由于我的应用程序测试在 CI 上使用最新版本的节点 10 运行,因此我想确保所有开发人员在运行任何 yarn 命令时都安装了最新的 10.xx版本。
For example, let's say the current latest node version is 10.22.1, then I want to stop the yarn install if the developer is on 10.22.0 or 10.11.1.例如,假设当前最新的节点版本是 10.22.1,那么如果开发人员在 10.22.0 或 10.11.1 上,我想停止纱线安装。
Using the engine directive in package.json I tried the following syntax but no avail.使用 package.json 中 的引擎指令我尝试了以下语法但无济于事。
{
"engines": {
"node": "^10.x.x",
}
}
{
"engines": {
"node": "^10",
}
}
{
"engines": {
"node": ">10.0.0 <11.0.0",
}
}
{
"engines": {
"node": "10",
}
}
All of these allow any node with major version 10.所有这些都允许任何具有主要版本 10 的节点。
As per the yarn documentation ( https://classic.yarnpkg.com/en/docs/package-json/ ), the preinstall
is called before the package is installed.根据纱线文档( https://classic.yarnpkg.com/en/docs/package-json/ ),在preinstall
包之前调用preinstall
安装。
If defined, the preinstall script is called by yarn before your package is installed.如果已定义,则在安装包之前由 yarn 调用预安装脚本。
So I would go with something like this in your package.json:所以我会在你的 package.json 中使用这样的东西:
"scripts": {
....
"preinstall": "./scripts/preinstall.sh",
}
Your preinstall.sh
could be:您的preinstall.sh
可以是:
#!/bin/bash
currentver="$(node -v)"
requiredver="v10.0.0"
if [ "$(printf '%s\n' "$requiredver" "$currentver" | sort -V | head -n1)" = "$requiredver" ]; then
echo "Version is good so let's go with install"
else
echo "Please install the node version greater than v10.0.0"
exit -1
fi
So if your developer has a version less than v10.0.0, the above script will fail and will in turn fail the yarn install
command.因此,如果您的开发人员的版本低于 v10.0.0,则上述脚本将失败,进而导致yarn install
命令失败。
Note: Credit to https://unix.stackexchange.com/questions/285924/how-to-compare-a-programs-version-in-a-shell-script for shell script for version comparison.注意:用于版本比较的 shell 脚本归功于 https://unix.stackexchange.com/questions/285924/how-to-compare-a-programs-version-in-a-shell-script 。
As we have in the npm doc :正如我们在npm 文档中所说:
to specify acceptable version ranges up to 1.0.4, use the following syntax:要指定最高 1.0.4 的可接受版本范围,请使用以下语法:
- Patch releases: 1.0 or 1.0.x or ~1.0.4补丁版本:1.0 或 1.0.x 或 ~1.0.4
- Minor releases: 1 or 1.x or ^1.0.4次要版本:1 或 1.x 或 ^1.0.4
- Major releases: * or x主要版本:* 或 x
So, if you want to ask for only the 10.22.1 version or newer you should use ~10.22.1 or ^10.22.1因此,如果您只想要求 10.22.1 或更新版本,则应使用 ~10.22.1 或 ^10.22.1
And it's another option to pin the version (you can read more about it from this link ) by using the exact version like:这是通过使用确切版本来固定版本的另一种选择(您可以从此链接阅读有关它的更多信息):
{
"engines": {
"node": "10.22.1",
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.