在Yum上升级Node.js的故事和解决方法

由于节点版本升级,曾经遇到过问题,需要说明问题的再现方法、原因和解决方法。

问题发生的环境

    • CentOS 6

CentOS release 6.9 (Final)
yum – 3.2.29

CentOS 7

CentOS Linux release 7.4.1708 (Core)
yum – 3.4.3

可能发生在其他操作系统上,但我没有进行确认。

在验证重现方法时,我们使用Node.js 6和7进行测试,但如果在其他版本的Node.js中以相同的方法进行导入,将会出现相同的问题。

问题1:无法安装新版本的nodejs。

再现方式

暂时先以root身份执行。
※即使用sudo执行,但由于我经常忘记输入sudo而发生此问题。

$ sudo su -

按照本网站上列出的规定,首先获取rpm文件。

$ curl --silent --location https://rpm.nodesource.com/setup_6.x | bash 
$ curl --silent --location https://rpm.nodesource.com/setup_7.x | bash 

为了方便再现,上述操作连续获取了两个rpm软件包,但假设我们希望安装7.x版本(即较新版本)。

然而,在这个时候,如果继续使用yum安装,

$ sudo yum -y install nodejs

我們選擇6系列。(・ω・ )

$ node -v
v6.12.3

问题的原因和解决方法

总之,就是有两个Node.js的rpm包,而且不小心使用了较旧版本的包作为优先版本。

$ ll /etc/yum.repos.d/ | grep node
-rw-r--r--. 1 root root 472 Oct 21  2016 nodesource-el6.repo
-rw-r--r--. 1 root root 472 Apr 26  2016 nodesource-el.repo

$ cat /etc/yum.repos.d/nodesource-el.repo 
[nodesource]
name=Node.js Packages for Enterprise Linux 6 - $basearch
baseurl=https://rpm.nodesource.com/pub_6.x/el/6/$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/NODESOURCE-GPG-SIGNING-KEY-EL

[nodesource-source]
name=Node.js for Enterprise Linux 6 - $basearch - Source
baseurl=https://rpm.nodesource.com/pub_6.x/el/6/SRPMS
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/NODESOURCE-GPG-SIGNING-KEY-EL
gpgcheck=1

所以,我们应该删除旧版本的rpm文件。

$ rm /etc/yum.repos.d/nodesource-el.repo

然后我们继续讨论问题二。

问题2:在安装过程中出现下载错误。

继续从问题1开始。

重现的方法

继续做问题1的基础上,为了暂时安装7系,需要先移除当前的node.js。

$ yum -y remove nodejs

然后重新安装。

$ yum -y install nodejs

那么就会发生错误。(・ω・)

Loaded plugins: fastestmirror, security
Setting up Install Process
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package nodejs.x86_64 2:6.12.3-1nodesource will be installed
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================================
 Package               Arch                  Version                                Repository                 Size
====================================================================================================================
Installing:
 nodejs                x86_64                2:6.12.3-1nodesource                   nodesource                 13 M

Transaction Summary
====================================================================================================================
Install       1 Package(s)

Total download size: 13 M
Installed size: 38 M
Downloading Packages:
https://rpm.nodesource.com/pub_7.x/el/6/x86_64/nodejs-6.12.3-1nodesource.x86_64.rpm: [Errno 14] PYCURL ERROR 22 - "The requested URL returned error: 404 Not Found"
Trying other mirror.
To address this issue please refer to the below knowledge base article 

https://access.redhat.com/articles/1320623

If above article doesn't help to resolve this issue please open a ticket with Red Hat Support.



Error Downloading Packages:
  2:nodejs-6.12.3-1nodesource.x86_64: failure: nodejs-6.12.3-1nodesource.x86_64.rpm from nodesource: [Errno 256] No more mirrors to try.

原因和解决方法

仔细查看错误信息后可以发现,它正在尝试向适用于nodejs7.x的URL发送nodejs6.x的请求,所以出现了没有这样的信息的错误(404 Not Found)。当然会出现这样的错误。

所以我要输入一些命令,似乎可以解决许多问题。

$ yum clean all

我将重新安装。

$ yum -y install nodejs

成功(・∀・)可以成功

$ node -v
v7.10.1

填補缺失

    • rpmの優先度の管理を、ファイルを直接触らずちゃんとコマンド経由などでやっていれば(そういうことができるのかどうかなどは調べていませんが)、もしかしたら問題2は起こらなかったのかなあなどとも思います。

 

    • ただ、rpmファイルの直接操作などしなくても問題2が起こる場合もありました。

操作ミスなども含む微妙な違いで起こったり起こらなかったりしたので、再現方法の特定はあきらめました。

请阅读以下内容。

    Node js package install for 7.x tries to install 6.9.5 for CentOS

还有其他很多地方我应该会参考到的资料,但是信息好像走丢了,只记下了笔记。非常感谢您!

广告
将在 10 秒后关闭
bannerAds