nvm 可以在统一系统中安装多个版本的Node.js的运行时,并在不同版本间快速切换,是前端开发的利器。
但是安装后,在使用时会有一些奇奇怪怪的问题,比如安装后还会报nvm command not found等,这里简单记录一下nvm的安装及问题的解决。
在官网下载的 node 安装包,运行后会自动安装在全局目录,使用过程中经常会遇到一些权限问题,所以推荐按照以下方法卸载全局安装的 node/npm
可能需要的额外指令
sudo rm /usr/local/bin/npm
sudo rm /usr/local/share/man/man1/node.1
sudo rm /usr/local/lib/dtrace/node.d
sudo rm -rf ~/.npm
sudo rm -rf ~/.node-gyp
sudo rm /opt/local/bin/node
sudo rm /opt/local/include/node
sudo rm -rf /opt/local/lib/node_modules安装脚本:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | 或者
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | 这两个脚本会下载nvm的仓库到~/.nvm目录,然后尝试把nvm命令加入到环境变量。我这里使用的是zsh,所以会加到~/.zshrc(脚本也会尝试加到~/.profile, ~/.bashrc故意~/.bash_profile)
安装后,要重新启动终端。 但是仍然可能在输入
nvm后提示nvm command not found
这种情况下,需要手动执行一下命令:
source ~/.zshrc
source ~/.nvm/nvm.sh手动够可以解决问题,但是只要重新打开终端,都要手动输入,实在过于麻烦。 因此,把这个指令放在 ~/.bashrc 或者 ~/.profile 或 ~/.zshrc。这样就可以在每次启动的时候,自动加载它。
我的shell使用的是zsh,因此,将命令source ~/.nvm/nvm.sh放入~/.zshrc中
vim ~/.zshrczsh还是有点简单,自动补全等功能不是很强大。因此安装oh my zsh来增强终端功能。
curl安装
sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"或者 wget安装
sh -c "$(wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"zsh-autosuggestions增强自动补全zsh-autosuggestions仓库到ohmyzsh的插件目录。(默认的是 ~/.oh-my-zsh/custom/plugins)git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestionsplugins=(pluginA pluginB... zsh-autosuggestions)注意,安装oh-my-zsh配置可能会被覆盖,可能需要重新把source ~/.nvm/nvm.sh命令放入~/.zshrc中