在 CentOS 下配置 Node.js 镜像,通常是为了加速 npm install 或 Node.js 本身的下载。下面按常见场景给你说明。
npm config set registry https://registry.npmmirror.com
验证:
npm config get registry
输出应为:
https://registry.npmmirror.com/
npm install --registry=https://registry.npmmirror.com
npm config set registry https://registry.npmjs.org
如果你是用 nvm 安装 Node.js,可以设置环境变量。
export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node
export NVM_IOJS_ORG_MIRROR=https://npmmirror.com/mirrors/iojs
永久生效(写入 ~/.bashrc 或 ~/.zshrc):
echo 'export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node' >> ~/.bashrc
echo 'export NVM_IOJS_ORG_MIRROR=https://npmmirror.com/mirrors/iojs' >> ~/.bashrc
source ~/.bashrc
然后重新安装 Node.js:
nvm install 18
wget https://npmmirror.com/mirrors/node/v18.19.0/node-v18.19.0-linux-x64.tar.xz
解压并配置环境变量:
tar -xJf node-v18.19.0-linux-x64.tar.xz
mv node-v18.19.0-linux-x64 /usr/local/node
echo 'export PATH=/usr/local/node/bin:$PATH' >> /etc/profile
source /etc/profile
验证:
node -v
npm -v
yarn config set registry https://registry.npmmirror.com
registrynpmmirrorNVM_NODEJS_ORG_MIRRORcurl -fsSL https://rpm.nodesource.com/setup_18.x | bash -
yum install -y nodejs
⚠️ 国内可能较慢,不如镜像方式稳定。
如果你愿意,可以告诉我:
我可以给你一套最适合你环境的配置方案。