温馨提示×

CentOS怎样配置Nodejs镜像

小樊
43
2026-08-18 14:09:44
栏目: 编程语言

在 CentOS 上配置 Node.js 镜像(国内加速),通常有两种常见需求:

  1. 使用 NodeSource 或官方二进制包时,配置国内镜像
  2. 使用 nvm 安装 Node.js 时配置镜像
  3. npm 使用国内镜像(如淘宝镜像)

下面分别说明。


一、使用 NodeSource 安装 Node.js(推荐方式)

1. 安装依赖

sudo yum install -y curl

2. 使用 NodeSource 镜像(清华源)

Node.js 18.x 为例:

curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/nodesource/rpm_18.x/setup | sudo bash -

然后安装:

sudo yum install -y nodejs

验证:

node -v
npm -v

二、使用 nvm 安装 Node.js(最灵活)

1. 安装 nvm

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

或国内镜像:

curl -o- https://gitee.com/mirrors/nvm/raw/master/install.sh | bash

2. 配置 nvm 使用国内镜像(关键)

编辑 ~/.bashrc~/.zshrc

export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node
export NVM_IOJS_ORG_MIRROR=https://npmmirror.com/mirrors/iojs

使配置生效:

source ~/.bashrc

3. 安装 Node.js

nvm install 18
nvm use 18

三、npm 配置国内镜像(非常重要)

1. 临时使用

npm install express --registry=https://registry.npmmirror.com

2. 永久配置

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

查看:

npm config get registry

四、yarn 镜像(如果你用 yarn)

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

五、常见问题

1. CentOS 7 已停止维护怎么办?

  • 建议升级到 Rocky Linux / AlmaLinux
  • 或使用 nvm + 二进制方式

2. node 命令找不到?

source ~/.bashrc

或检查 PATH:

echo $PATH

六、推荐组合(国内服务器)

nvm + npmmirror

export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node
nvm install 18
npm config set registry https://registry.npmmirror.com

如果你愿意,可以告诉我:

  • CentOS 版本(7 / 8 / Stream)
  • 是否生产环境
  • 是否需要 Docker 方式

我可以给你一套最适合你环境的配置方案

0