温馨提示×

centos nodejs怎么配置环境

小樊
38
2026-09-30 00:04:05
栏目: 编程语言

在 CentOS 上配置 Node.js 环境,常见、稳妥的方式有下面几种。我按推荐程度给你讲,并附上具体命令。


一、推荐方式:使用 NodeSource 安装(版本新、好管理)

1. 更新系统

sudo yum update -y

2. 安装依赖

sudo yum install -y gcc-c++ make

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

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

想装其他版本,把 18.x 改成 20.x / 16.x 等

4. 安装 Node.js

sudo yum install -y nodejs

5. 验证

node -v
npm -v

二、简单方式:使用 yum 自带源(版本较旧)

sudo yum install -y nodejs npm
node -v

⚠️ CentOS 7 默认源 Node 版本很低(如 6.x / 8.x),不推荐生产使用


三、灵活方式:使用 NVM(适合开发)

1. 安装 NVM

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

2. 重载环境

source ~/.bashrc

3. 安装 Node

nvm install 18
nvm use 18

✅ 优点:可多版本切换
❌ 缺点:只对当前用户生效


四、配置 npm(可选但推荐)

设置国内镜像(加速)

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

查看配置

npm config get registry

五、常见问题

1. node: command not found

  • 确认是否 source ~/.bashrc
  • 或重新登录 shell

2. CentOS 6?

⚠️ 已停止维护,不建议使用

3. 防火墙 / 端口

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

如果你告诉我:

  • ✅ CentOS 版本(7 / 8 / Stream)
  • ✅ 用途(学习 / 生产 / 前端 / 后端)
  • ✅ 是否用 Docker

我可以直接给你最合适的完整配置方案。

0 踩