nvm 可以切换 nodejs 版本
使用git 方式安装nvm
方法:https://github.com/creationix/nvm#git-install
前提已安装git
cd ~/
- -切到主目录git clone https://github.com/creationix/nvm.git .nvm
- -克隆代码到文件夹 .nvmcd ~/.nvm
- -进入nvm代码目录git checkout v0.33.11
- -切换到v0.33.11版本source ~/.nvm/nvm.sh
- -执行命令
如果需要安装最新版本:
[root@localhost .nvm]# nvm --version
0.33.11
[root@localhost .nvm]# git fetch -p
[root@localhost .nvm]# git rev-list --tags --max-count=1
258938ef66a2a49a4a400554a6dce890226ae34c
[root@localhost .nvm]# git describe --tags 258938ef66a2a49a4a400554a6dce890226ae34c
v0.35.3
[root@localhost .nvm]# git describe --abbrev=0 --tags
v0.35.3
[root@localhost .nvm]# git checkout $(git describe --tags `git rev-list --tags --max-count=1`)
Note: checking out 'v0.35.3'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b new_branch_name
HEAD is now at 258938e... v0.35.3
[root@localhost .nvm]# source ~/.nvm/nvm.sh
[root@localhost .nvm]# nvm --version
0.35.3
解决下载缓慢问题,设置国内镜像
export NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node
将nvm添加到环境
echo "source ~/nvm/nvm.sh" >> ~/.bashrc
下面几个简单常用的命令:
查看远程版本
nvm ls-remote
安装版本
nvm install 10.9.0
切换版本
nvm use 10.9.0
查看所有版本
nvm ls
除了该方法,网上还有其他方法,但没尝试过:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
或者
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
安装nrm
已经安装完node之后,npm安装包下载都会缓慢,则需要切换其下载源,于是nrm来了
安装简单,直接:
npm i nrm -g
查看源
[root@localhost ~]# nrm ls
npm -------- https://registry.npmjs.org/
yarn ------- https://registry.yarnpkg.com/
cnpm ------- http://r.cnpmjs.org/
* taobao ----- https://registry.npm.taobao.org/
nj --------- https://registry.nodejitsu.com/
npmMirror -- https://skimdb.npmjs.com/registry/
edunpm ----- http://registry.enpmjs.org/
添加自己的私服
[root@localhost ~]# nrm add private http://10.10.2.14:8081/repository/
add registry private success
[root@localhost ~]# nrm use private
Registry has been set to: http://10.10.2.14:8081/repository/
安装pm2
部署 node 应用,除了docker,pm2是一个不错的选择。
安装简单:
npm i pm2 -g
[root@localhost ~]# pm2 ls
┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name │ mode │ ↺ │ status │ cpu │ memory │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘