温馨提示×

怎样在centos设nodejs

小樊
39
2026-09-08 21:12:32
栏目: 编程语言

在 CentOS 上安装和配置 Node.js,常见有以下几种方式。你可以根据需求选择。


一、使用 NodeSource 仓库安装(推荐,版本较新)

1. 更新系统

sudo yum update -y

2. 安装依赖

sudo yum install -y curl

3. 添加 NodeSource 源(以 Node.js 18 为例)

curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -

如需其他版本,把 18.x 改成 16.x、20.x 等

4. 安装 Node.js

sudo yum install -y nodejs

5. 验证

node -v
npm -v

二、使用 CentOS 自带仓库(版本较旧)

sudo yum install -y nodejs npm

适合对版本要求不高的场景。


三、使用 NVM 安装(推荐开发环境,可多版本切换)

1. 安装 NVM

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

2. 重新加载 shell

source ~/.bashrc

3. 安装 Node.js

nvm install 18
nvm use 18

4. 设置默认版本

nvm alias default 18

四、配置国内镜像(加速 npm)

npm config set registry https://registry.npmmirror.com

五、常见问题

1. 防火墙放行端口(如 3000)

sudo firewall-cmd --permanent --add-port=3000/tcp
sudo firewall-cmd --reload

2. 使用 PM2 守护进程

npm install -g pm2
pm2 start app.js

如果你能告诉我:

  • CentOS 版本(7 / 8 / Stream)
  • 用途(生产 / 开发)
  • 是否需要多版本

我可以给你更精确的安装方案。

0 踩