在Centos6或7上安装nodejs最简单的方式还是yum,但是默认epel源中nodejs版本过旧,本文介绍下新版nodejs安装方法。
方法来自:https://github.com/nodesource/distributions
1、安装NodeJS10.x
curl -sL https://rpm.nodesource.com/setup_10.x | bash - yum install nodejs
|-----|-----------------------------------------------------------------------------| | 1 2 | curl -sL https://rpm.nodesource.com/setup_10.x | bash - yum install nodejs |
2、查看版本
node -v v10.16.3
|-----|------------------| | 1 2 | node -v v10.16.3 |
npm -v 6.9.0
|-----|--------------| | 1 2 | npm -v 6.9.0 |
3、更换镜像(可选)
更换淘宝镜像
npm config set registry https://registry.npm.taobao.org
|---|---------------------------------------------------------| | 1 | npm config set registry https://registry.npm.taobao.org |
查看是否更换成功
npm config get registry
|---|-------------------------| | 1 | npm config get registry |
更换回官方源
npm config set registry https://registry.npmjs.org
|---|----------------------------------------------------| | 1 | npm config set registry https://registry.npmjs.org |
附:安装其他版本的NodeJS
NodeJS 12.x
curl -sL https://rpm.nodesource.com/setup_12.x | bash - yum install nodejs
|-----|-----------------------------------------------------------------------------| | 1 2 | curl -sL https://rpm.nodesource.com/setup_12.x | bash - yum install nodejs |
NodeJS 11.x
curl -sL https://rpm.nodesource.com/setup_11.x | bash - yum install nodejs
|-----|-----------------------------------------------------------------------------| | 1 2 | curl -sL https://rpm.nodesource.com/setup_11.x | bash - yum install nodejs |
NodeJS 8.x
curl -sL https://rpm.nodesource.com/setup_8.x | bash - yum install nodejs
|-----|----------------------------------------------------------------------------| | 1 2 | curl -sL https://rpm.nodesource.com/setup_8.x | bash - yum install nodejs |