20190315 使用 nvm 安装 nodejs
1、 找到 nvm 官网帮助
https://github.com/creationix/nvm
我习惯于 terminal 操作,所以选择 git install ,帮助文档如下https://github.com/creationix/nvm#git-install
2、下载 nvm、install
mkdir nvm
cd nvm/
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
提示内容:
=> Downloading nvm as script to '/home/dhbm/.nvm'
=> Appending nvm source string to /home/dhbm/.bashrc
=> Appending bash_completion source string to /home/dhbm/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
3、设置环境
按照提示,直接 copy 粘贴以上内容
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
确认 nvm
command -v nvm
返回结果:
nvm
4、安装 nodejs
nvm install node
返回内容如下:
Downloading and installing node v11.11.0..
...
确认 nodejs 和 npm 版本
node --version
返回结果:
v11.11.0
npm -v
6.7.0
5、设置 淘宝镜像代理
获取当前 npm 代理
npm get registry
返回结果:
https://registry.npmjs.org/
设置淘宝镜像代理
npm config set registry http://registry.npm.taobao.org/
恢复 npm 代理
npm config set registry https://registry.npmjs.org/
6、安装 cnpm
npm install cnpm -g
查看版本号
cnpm -v
返回结果:
[email protected] (/home/dhbm/.nvm/versions/node/v11.11.0/lib/node_modules/cnpm/lib/parse_argv.js)
[email protected] (/home/dhbm/.nvm/versions/node/v11.11.0/lib/node_modules/cnpm/node_modules/npm/lib/npm.js)
[email protected] (/home/dhbm/.nvm/versions/node/v11.11.0/bin/node)
[email protected] (/home/dhbm/.nvm/versions/node/v11.11.0/lib/node_modules/cnpm/node_modules/npminstall/lib/index.js)
prefix=/home/dhbm/.nvm/versions/node/v11.11.0
linux x64 4.15.0-46-generic
registry=https://registry.npm.taobao.org
7、安装 yarn
经常有 npm 找不到的包, npm淘宝镜像和 cnpm 也不管用
所以,也安装一个 yarn 备用
npm install -g yarn
查看版本号
yarn -v
1.13.0
同样设置一下 yarn 淘宝镜像代理
yarn config set registry http://registry.npm.taobao.org/
yarn config set registry https://registry.npm.taobao.org -g
yarn config set sass_binary_site http://cdn.npm.taobao.org/dist/node-sass -g