在 CentOS 上配置 Node.js 环境,常见、稳妥的方式有下面几种。我按推荐程度给你讲,并附上具体命令。
sudo yum update -y
sudo yum install -y gcc-c++ make
curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -
想装其他版本,把
18.x改成20.x/16.x等
sudo yum install -y nodejs
node -v
npm -v
sudo yum install -y nodejs npm
node -v
⚠️ CentOS 7 默认源 Node 版本很低(如 6.x / 8.x),不推荐生产使用
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install 18
nvm use 18
✅ 优点:可多版本切换
❌ 缺点:只对当前用户生效
npm config set registry https://registry.npmmirror.com
npm config get registry
node: command not foundsource ~/.bashrc⚠️ 已停止维护,不建议使用
sudo firewall-cmd --add-port=3000/tcp --permanent
sudo firewall-cmd --reload
如果你告诉我:
我可以直接给你最合适的完整配置方案。