跳到主要内容
方的代码随记
返回

CentOS 搭建 Node.js 环境

更新于:

二进制包安装

# 下载安装包(也可换 v15.14.0 / v20.10.0)
wget https://npm.taobao.org/mirrors/node/v12.4.0/node-v12.4.0-linux-x64.tar.xz
# wget https://nodejs.org/dist/v15.14.0/node-v15.14.0-linux-x64.tar.gz
# wget https://nodejs.org/dist/v20.10.0/node-v20.10.0-linux-x64.tar.gz

# 解压并重命名到安装目录
tar -xvf node-v12.4.0-linux-x64.tar.xz
mv node-v12.4.0-linux-x64/ /usr/local/node

# 配置环境变量
echo "export PATH=$PATH:/usr/local/node/bin" >> /etc/profile
source /etc/profile

# 验证
node -v
npm -v

npm 镜像源

# 查看当前镜像源
npm get registry

# 设置淘宝镜像源
npm config set registry https://registry.npm.taobao.org/

# 切换回官方镜像源
npm config set registry http://www.npmjs.org/

用 nvm 管理多版本

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
# 或
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash

安装后按 shell 类型加载:

bash: source ~/.bashrc
zsh:  source ~/.zshrc
ksh:  . ~/.profile

切换版本:

$ nvm use 16
Now using node v16.9.1 (npm v7.21.1)
$ nvm use 14
Now using node v14.18.0 (npm v6.14.15)
$ nvm install 12
Now using node v12.22.6 (npm v6.14.5)

nrm 镜像源管理工具

npm install -g nrm
nrm ls        # 查看可用镜像源
nrm use cnpm  # 切换到指定镜像源

registry.npm.taobao.org 已逐步下线,新环境建议改用 registry.npmmirror.com。


分享这篇文章:

上一篇
PHP 安装的四种方式
下一篇
CentOS 7 源码编译安装 Git