简体   繁体   English

Yarn 安装 - 如何强制节点 (10.xx) 的最新次要版本和补丁

[英]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.

相关问题 如何保证所有请求都异步完成,map 请求 url 到 promise 响应 Z3B2819DD4C2449552FZ - How to guarantee all requests are fulfilled asynchronously, and map the request url to the promise response in Node.js 10.x.x? 如何npm安装并强制使用最新版本(永久)? - How to npm install and force latest version (permanently)? 如何使用纱线将软件包升级到最新版本? - How to upgrade package to latest version using yarn? 如何在ubuntu中安装特定的纱线版本 - How to install particular yarn version in ubuntu 如何仅在节点8.7.x上npm安装devDependencies? - how to npm install only devDependencies with node 8.7.x? 如何在D3.js版本4中访问节点x和y? - How to access node x and y in D3.js version 4? 如何强制ojet安装Node-sass版本4.9而不是默认版本(4.7)? - How to force ojet to install node-sass version 4.9 instead of the default version (4.7)? package.json 中的节点引擎 8.x 或 10.x - Node engine 8.x or 10.x in package.json 'npm 是否安装'l<package_name> 安装一个package的最新版本还是安装最新版本的node兼容?</package_name> - Does 'npm instal'l <package_name> install the latest version of a package or the latest compatible with the version of node installed? 如何使用最新的Browserify(6.xx)创建供应商捆绑包? - How to create vendor bundles with latest Browserify (6.x.x)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM