CentOS安装nodejs并配置cnpm
写在前面
方法1是先安装版本控制脚本(如果失败,请尝试方法2)
方法1
$curl -L https://raw.githubusercontent.com/mklement0/n-install/stable/bin/n-install | bash
$source ~/.bash_profile
$n stable # 自动安装稳定版
$n latest # 自动安装最新版
$n ls # 列出已下载的版本
$n 版本号 # 切换版本,没有会自动下载
$n rm 版本号 # 删除版本
$n --help # 帮助信息
方法2
$curl -sL https://rpm.nodesource.com/setup_8.x | bash -
$sudo yum install -y nodejs
$node -v; npm -v
v8.17.0
6.13.4
-
更改npm淘宝镜像,以后使用cnpm替代npm
$sudo npm install -g cnpm --registry=https://registry.npm.taobao.org
-
安装node版本管理模块n
$sudo cnpm install n -g
-
安装LTS版node并保留原npm和npx
$sudo n -p lts installed : v16.13.2 Note: the node command changed location and the old location may be remembered in your current shell. old : /usr/bin/node new : /usr/local/bin/node If "node --version" shows the old version then start a new shell, or reset the location hash with: hash -r (for bash, zsh, ash, dash, and ksh) rehash (for csh and tcsh)
-
读以上安装后的信息可知新版本的node安装后未及时生效,需要刷新当前session
$hash -r