为什么在 CentOS 7 上不能安装正确的 Node.js 新版本?

Cod*_*Med 15 yum rhel centos node.js

注意:这已部分解决,但需要多加注意。请参阅 OP 底部的注释和@Digisec 的部分答案。

我正在尝试在 CentOS 7 上设置 Node.js,但以下命令无法安装最新版本。 为了在 CentOS 7 上安装最新版本的 Node.js,我需要更改以下代码中的哪些内容?

我首先输入node --version结果为v0.12.7. 这显然是一个旧版本,因为nodejs.org说这v5.6.0是最新的稳定版本。

然后我输入以下内容以获取最新版本的 node.js:

cd /tmp
curl -sL https://rpm.nodesource.com/setup | bash -
Run Code Online (Sandbox Code Playgroud)

终端打印出我应该键入以下内容以删除过时的 node.js 版本,因此我键入以下内容:

yum remove -y nodejs npm
Run Code Online (Sandbox Code Playgroud)

终端还说要输入以下内容来安装最新版本的 node.js,所以我输入以下内容:

yum install -y nodejs
Run Code Online (Sandbox Code Playgroud)

但是我node --version再次输入,终端v0.12.7再次回复,显示前面的命令,只是重新安装了旧的过时版本。

那么我需要做什么才能在 CentOS 7 机器上安装最新的稳定版本的 node.js?


正在进行的努力:


根据@Digisec 的建议,我重新运行了上述命令,但这次将所有输出记录到以下文本中。请注意,现在我再次运行它的顺序略有不同,但结果是一样的,如下:

[root@localhost ~]# node --version
v0.12.7
[root@localhost ~]# cd /tmp
[root@localhost tmp]# curl -sL https://rpm.nodesource.com/setup | bash -

## Installing the NodeSource Node.js 0.10 repo...


## Inspecting system...

+ rpm -q --whatprovides redhat-release || rpm -q --whatprovides centos-release || rpm -q --whatprovides cloudlinux-release || rpm -q --whatprovides sl-release
+ uname -m

## Confirming "el7-x86_64" is supported...

+ curl -sLf -o /dev/null 'https://rpm.nodesource.com/pub_0.10/el/7/x86_64/nodesource-release-el7-1.noarch.rpm'

## Downloading release setup RPM...

+ mktemp
+ curl -sL -o '/tmp/tmp.ePYEdVWXQH' 'https://rpm.nodesource.com/pub_0.10/el/7/x86_64/nodesource-release-el7-1.noarch.rpm'

## Installing release setup RPM...

+ rpm -i --nosignature --force '/tmp/tmp.ePYEdVWXQH'

## Cleaning up...

+ rm -f '/tmp/tmp.ePYEdVWXQH'

## Checking for existing installations...

+ rpm -qa 'node|npm' | grep -v nodesource

## Run `yum install -y nodejs` (as root) to install Node.js 0.10 and npm.
## You may also need development tools to build native addons:
##   `yum install -y gcc-c++ make`

[root@localhost tmp]# yum remove -y nodejs npm
Loaded plugins: fastestmirror, langpacks
No Match for argument: npm
Resolving Dependencies
--> Running transaction check
---> Package nodejs.x86_64 0:0.10.42-1nodesource.el7.centos will be erased
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================================================================================================
 Package                             Arch                                Version                                                     Repository                                Size
====================================================================================================================================================================================
Removing:
 nodejs                              x86_64                              0.10.42-1nodesource.el7.centos                              @nodesource                               16 M

Transaction Summary
====================================================================================================================================================================================
Remove  1 Package

Installed size: 16 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Erasing    : nodejs-0.10.42-1nodesource.el7.centos.x86_64                                                                                                                         1/1 
  Verifying  : nodejs-0.10.42-1nodesource.el7.centos.x86_64                                                                                                                         1/1 

Removed:
  nodejs.x86_64 0:0.10.42-1nodesource.el7.centos                                                                                                                                    

Complete!
[root@localhost tmp]# yum install -y nodejs
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: www.gtlib.gatech.edu
 * epel: mirror.sfo12.us.leaseweb.net
 * extras: dallas.tx.mirror.xygenhosting.com
 * updates: linux.mirrors.es.net
Resolving Dependencies
--> Running transaction check
---> Package nodejs.x86_64 0:0.10.42-1nodesource.el7.centos will be installed
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================================================================================================
 Package                             Arch                                Version                                                      Repository                               Size
====================================================================================================================================================================================
Installing:
 nodejs                              x86_64                              0.10.42-1nodesource.el7.centos                               nodesource                              4.5 M

Transaction Summary
====================================================================================================================================================================================
Install  1 Package

Total download size: 4.5 M
Installed size: 16 M
Downloading packages:
nodejs-0.10.42-1nodesource.el7.centos.x86_64.rpm                                                                                                             | 4.5 MB  00:00:09     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : nodejs-0.10.42-1nodesource.el7.centos.x86_64                                                                                                                         1/1 
  Verifying  : nodejs-0.10.42-1nodesource.el7.centos.x86_64                                                                                                                         1/1 

Installed:
  nodejs.x86_64 0:0.10.42-1nodesource.el7.centos                                                                                                                                    

Complete!
[root@localhost tmp]# node --version
v0.12.7
[root@localhost tmp]#   
Run Code Online (Sandbox Code Playgroud)

然后,根据@Digisec 的回答,我接下来尝试了以下操作,但结果仍然无法安装正确的版本,正如您从以下终端输出中看到的:

[root@localhost tmp]# curl --silent --location https://rpm.nodesource.com/setup_5.x | bash -

## Installing the NodeSource Node.js 5.x repo...


## Inspecting system...

+ rpm -q --whatprovides redhat-release || rpm -q --whatprovides centos-release || rpm -q --whatprovides cloudlinux-release || rpm -q --whatprovides sl-release
+ uname -m

## Confirming "el7-x86_64" is supported...

+ curl -sLf -o /dev/null 'https://rpm.nodesource.com/pub_5.x/el/7/x86_64/nodesource-release-el7-1.noarch.rpm'

## Downloading release setup RPM...

+ mktemp
+ curl -sL -o '/tmp/tmp.NEM1bxM9WB' 'https://rpm.nodesource.com/pub_5.x/el/7/x86_64/nodesource-release-el7-1.noarch.rpm'

## Installing release setup RPM...

+ rpm -i --nosignature --force '/tmp/tmp.NEM1bxM9WB'

## Cleaning up...

+ rm -f '/tmp/tmp.NEM1bxM9WB'

## Checking for existing installations...

+ rpm -qa 'node|npm' | grep -v nodesource

## Run `yum install -y nodejs` (as root) to install Node.js 5.x and npm.
## You may also need development tools to build native addons:
##   `yum install -y gcc-c++ make`

[root@localhost tmp]# yum remove -y nodejs npm
Loaded plugins: fastestmirror, langpacks
No Match for argument: npm
Resolving Dependencies
--> Running transaction check
---> Package nodejs.x86_64 0:0.10.42-1nodesource.el7.centos will be erased
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================================================================================================
 Package                             Arch                                Version                                                     Repository                                Size
====================================================================================================================================================================================
Removing:
 nodejs                              x86_64                              0.10.42-1nodesource.el7.centos                              @nodesource                               16 M

Transaction Summary
====================================================================================================================================================================================
Remove  1 Package

Installed size: 16 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Erasing    : nodejs-0.10.42-1nodesource.el7.centos.x86_64                                                                                                                             1/1 
  Verifying  : nodejs-0.10.42-1nodesource.el7.centos.x86_64                                                                                                                             1/1 

Removed:
  nodejs.x86_64 0:0.10.42-1nodesource.el7.centos                                                                                                                                    

Complete!
[root@localhost tmp]# yum install -y nodejs
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: www.gtlib.gatech.edu
 * epel: mirror.sfo12.us.leaseweb.net
 * extras: dallas.tx.mirror.xygenhosting.com
 * updates: linux.mirrors.es.net
Resolving Dependencies
--> Running transaction check
---> Package nodejs.x86_64 0:0.10.42-1nodesource.el7.centos will be installed
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================================================================================================
 Package                             Arch                                Version                                                      Repository                               Size
====================================================================================================================================================================================
Installing:
 nodejs                              x86_64                              0.10.42-1nodesource.el7.centos                               nodesource                              4.5 M

Transaction Summary
====================================================================================================================================================================================
Install  1 Package

Total download size: 4.5 M
Installed size: 16 M
Downloading packages:
nodejs-0.10.42-1nodesource.el7 FAILED                                          
https://rpm.nodesource.com/pub_5.x/el/7/x86_64/nodejs-0.10.42-1nodesource.el7.centos.x86_64.rpm: [Errno 14] HTTPS Error 404 - Not Found           ]  0.0 B/s |    0 B  --:--:-- ETA 
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 create a bug on https://bugs.centos.org/



Error downloading packages:
  nodejs-0.10.42-1nodesource.el7.centos.x86_64: [Errno 256] No more mirrors to try.

[root@localhost tmp]# yum install -y nodejs
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: www.gtlib.gatech.edu
 * epel: mirror.sfo12.us.leaseweb.net
 * extras: dallas.tx.mirror.xygenhosting.com
 * updates: linux.mirrors.es.net
Resolving Dependencies
--> Running transaction check
---> Package nodejs.x86_64 0:0.10.42-1nodesource.el7.centos will be installed
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================================================================================================
 Package                             Arch                                Version                                                      Repository                               Size
====================================================================================================================================================================================
Installing:
 nodejs                              x86_64                              0.10.42-1nodesource.el7.centos                               nodesource                              4.5 M

Transaction Summary
====================================================================================================================================================================================
Install  1 Package

Total download size: 4.5 M
Installed size: 16 M
Downloading packages:
No Presto metadata available for nodesource  
nodejs-0.10.42-1nodesource.el7 FAILED    
https://rpm.nodesource.com/pub_5.x/el/7/x86_64/nodejs-0.10.42-1nodesource.el7.centos.x86_64.rpm: [Errno 14] HTTPS Error 404 - Not Found           ]  0.0 B/s |    0 B  --:--:-- ETA 
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 create a bug on https://bugs.centos.org/



Error downloading packages:
  nodejs-0.10.42-1nodesource.el7.centos.x86_64: [Errno 256] No more mirrors to try.

[root@localhost tmp]# node --version
v0.12.7
[root@localhost tmp]# 
Run Code Online (Sandbox Code Playgroud)

然后我访问了上面错误日志引用的知识库文章,但发现我无法访问完整的文章,因为我不是 Red Had 客户。


部分解决方案:


@Digisec 通过展示如何安装新版本部分解决了这个问题,但我们仍然无法删除旧版本,这将留下冲突的风险。 需要采取哪些步骤来删除旧版本,以便新安装可以干净完整?

在此链接中找到了旧版本 v0.12.7 的 tar 网址。而且@Digisec' 还建议阅读其他链接,该链接建议使用命令make uninstall. 我觉得可能只有五到十个命令可以在终端中键入以删除旧的 v0.12.7 并重新安装新版本的 nodejs。什么是必需的命令?

我似乎在 CentOS 7 机器上使用以下终端命令找到了与 nodejs v0.12.7 相关的三个 tar 文件:

[root@localhost ~]# cd /
[root@localhost /]# find . -type f -iname "*.tar"
find: ‘./run/user/1000/gvfs’: Permission denied
./opt/node-v0.12.7/deps/npm/node_modules/tar/test/dir-normalization.tar
./opt/node-v0.12.7/deps/npm/test/fixtures/gitignore-and-npmignore-2.tar
./opt/node-v0.12.7/deps/npm/test/fixtures/gitignore-and-npmignore.tar
./usr/local/lib/node_modules/npm/test/fixtures/gitignore-and-npmignore-2.tar
./usr/local/lib/node_modules/npm/test/fixtures/gitignore-and-npmignore.tar
./usr/local/lib/node_modules/npm/node_modules/tar/test/dir-normalization.tar
[root@localhost /]# 
Run Code Online (Sandbox Code Playgroud)

请注意,还有另外三个 tar 文件标记了node_modules与它们的关系v0.12.7尚不清楚。

我运行哪些特定命令来安全地从机器上卸载v0.12.7和删除所有v0.12.7相关的 tar 文件?

Dig*_*sec 8

我刚刚检查了您从中获得的链接。您正在安装,特别是 version 0.10,或者至少尝试安装,但是您正在安装的存储库中有一个更新的版本。您必须遵循节点 v5 的设置。

curl --silent --location https://rpm.nodesource.com/setup_5.x | bash -
Run Code Online (Sandbox Code Playgroud)

PS:为了将来参考,请确保您非常仔细地阅读这些站点,尤其是在以root身份运行命令时。

编辑: 好的,您似乎正确添加了repo。查看 repo,您​​的软件包在那里,但系统正在寻找错误的版本。在这一点上,我会建议。

yum clean all && yum update
Run Code Online (Sandbox Code Playgroud)

这将清除缓存的 repo 并从远程抓取。

回答: 在我们完成了对 的工作之后chat,这是给您的正确答案。nodejs v5安装好了。您之前nodejs v0.12手动编译,这在二进制发行版上不是一个好主意,因为您最终会处于这个位置。

which node
Run Code Online (Sandbox Code Playgroud)

回来

/usr/local/bin/node
Run Code Online (Sandbox Code Playgroud)

尽管

/usr/bin/node
Run Code Online (Sandbox Code Playgroud)

返回了 的正确答案v5.x

在这一点上,您有多种选择来修复它,但我建议使用 2。

  • 选项 1:这是最简单的方法。您可以编辑您的.bashrc并添加一个alias将指向node -> /usr/bin/node. 这就像添加alias node="/usr/bin/node". 这每次都会调用正确的版本,但我不能保证将来不会与库发生冲突。
  • 选项 2:这是一种艰难的方法,但它会解决它。首先,使用yum系统上安装的 nodejs 进行清除。其次,获取已编译版本的 tarball,解压缩./configure并卸载它。这一步,你可以在网上轻松找到。最后,nodejs通过重新安装yum,这应该可以解决您的问题。


小智 5

我做了什么:

rm -f /etc/yum.repos.d/nodesource-el*
curl -sL https://rpm.nodesource.com/setup_7.x | 重击——
yum install -y nodejs
节点 --version

我得到了:

v7.2.0