CentOS 无法通过 yum 安装 Node.js。

10

我正在使用 CentOS 服务器上的 Node v0.10.x,并想要更新 Node.js。因此,按照一些文章进行操作。首先需要卸载当前安装的版本:

# which node
# cd /usr
# rm -r bin/node bin/node-waf include/node lib/node lib/pkgconfig/nodejs.pc share/man/man1/node.1

然后检查 node -v 不起作用,很好。但使用 yum 不能安装最新版本的 Node.js,所以我搜索了一些文章并找到了这篇文章:https://www.metachris.com/2015/10/how-to-install-nodejs-5-on-centos-and-ubuntu/

于是我按照以下命令进行操作:

# rpm -Uvh https://rpm.nodesource.com/pub_5.x/el/7/x86_64/nodesource-release-el7-1.noarch.rpm
# yum install nodejs -y

这就是问题所在。它打印了许多错误并且一直失败。

Resolving Dependencies
--> Running transaction check
---> Package nodejs.x86_64 0:0.10.42-4.el6 will be updated
--> Processing Dependency: nodejs(x86-64) = 0.10.42-4.el6 for package:     nodejs-devel-0.10.42-4.el6.x86_64
---> Package nodejs.x86_64 0:5.11.1-1nodesource.el7.centos will be an update
--> Running transaction check
---> Package nodejs-devel.x86_64 0:0.10.42-4.el6 will be updated
---> Package nodejs-devel.x86_64 0:5.11.1-1nodesource.el7.centos will be an update
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package         Arch      Version                          Repository     Size
================================================================================
Updating:
 nodejs          x86_64    5.11.1-1nodesource.el7.centos    nodesource    8.7 M
Updating for dependencies:
 nodejs-devel    x86_64    5.11.1-1nodesource.el7.centos    nodesource    7.6 M

Transaction Summary
================================================================================
Upgrade  1 Package (+1 Dependent package)

Total size: 16 M
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test


Transaction check error:
  file /usr/lib/node_modules/npm/node_modules/semver/package.json from install of nodejs-5.11.1-1nodesource.el7.centos.x86_64 conflicts with file from package nodejs-semver-2.1.0-1.el6.noarch
  file /usr/lib/node_modules/npm/node_modules/semver/bin/semver from     install of nodejs-5.11.1-1nodesource.el7.centos.x86_64 conflicts with file from package nodejs-semver-2.1.0-1.el6.noarch
  file /usr/lib/node_modules/npm/node_modules/semver/semver.js from install of nodejs-5.11.1-1nodesource.el7.centos.x86_64 conflicts with file from package nodejs-semver-2.1.0-1.el6.noarch
  ... and keeps going on ...

在删除节点之前,我还做了以下操作:
# npm cache clean -f
# npm install -g n
# n stable

但这完全没有起作用,因为它总是显示版本0.10.x,即使已经下载了最新的版本!(我看到了6.2.2或类似的版本) 我认为我的服务器出了些问题,但我不知道如何解决。有人能给我一些建议吗?如果可以的话,我想安装最新的Node.js。

1
你可以尝试移除包nodejs-semver-2.1.0-1.el6.noarch - J.J. Hakala
@J.J.Hakala find / -name nodejs-semver-2.1.0-1.el6.noarch 结果为空。那是一个文件吗? - modernator
1
@modernator,rpm软件包不会被保存为软件包,它们会被安装到目标位置(/usr/),然后从缓存中删除。... 卸载:# yum remove nodejs-semver ... - Knud Larsen
4个回答

20

从EPEL仓库安装Node

yum update
yum install epel-release
yum install nodejs
node --version

为了管理他们的Node包,访问npm。

yum install npm

2
嗨@Haris,当我运行上述命令时,出现“没有可用的epel-release软件包”。 - Naveen Kumar
@NaveenKumar,目前我在安装nodejs时遇到了相同的问题“没有epel-release软件包”,你能解决吗? - user3534759
@user3534759 是的,我直接从Node网站下载了软件包,并将其添加到了PATH中。 - Naveen Kumar
这在2023年可以在Centos 7上运行。 - Fahad

11

最简单的方法是使用nvm,即"Node Version Manager"。

在 shell 中输入:

$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

注意: 如果您想在运行脚本之前了解其功能,请阅读nvm github页面上的文档,或从curl命令中删除 | bash以在不运行.sh文件的情况下进行阅读

关闭并重新打开shell以使用更新的配置文件。然后:

//prints "nvm" if correctly installed
$ command -v nvm

// install latest version of node
$ nvm install node

//check it's installed correctly
$ node -v
v12.6.0

尝试了其他方法后,这个没有问题。


安装完npm后,请运行source ~/.nvm/nvm.sh,以确保git获取nvm命令按预期工作。 - Long Nguyen
1
这适用于Oracle Cloud和所有不同的服务器。你救了我的命,我实际上花了一个月来修复它。 - Software Engineer

2
首先,您必须安装epe-release。
sudo yum install epel-release

之后,您可以安装node.js和npm。
sudo yum -y install nodejs npm

它对我很有用。


3
我发现在我的CentOS 7上找不到相应的软件包:nodejs和npm。 - Jeff Bootsholz
@RajuyourPepe,以下文章适合你阅读: https://tecadmin.net/install-latest-nodejs-and-npm-on-centos/ - Ninja

0

我在一台Fedora 35工作站上(从Fedora 34升级而来)花了一段时间。

$ yum repolist
nodesource                  Node.js Packages for Fedora Linux 34 - x86_64

它给了我

$ node --version       v12.22.1 

在我找到这个解释之前,没有一个安装程序起到了作用:

如何在Fedora 36/35/34上安装Node.js

该教程包括3种方法。对我有用的是第三种方法。

通过NVM在Fedora上安装Node.js

首先,在系统上安装NVM工具,运行以下命令:

$ curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash   

然后,使用此命令重新加载系统环境。它将设置所需的环境变量以在系统上使用nvm。

$ source ~/.bashrc 
$ nvm install v16.14 
Downloading and installing node v16.14.2...


$ node --version 
v16.14.2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接