node版本切换工具nvs使用
直接从 GitHub clone nvs 到本地, 这里假设大家都使用 $HOME/git 目录存放 git 项目:
export NVS_HOME="$HOME/.nvs"
git clone https://github.com/jasongin/nvs "$NVS_HOME"
. "$NVS_HOME/nvs.sh" install
其中 . “$NVS_HOME/nvs.sh” install 一行命令会自动将 nvs 设置到你的 ~/.bashrc, ~/.profile, ~/.bash_profile, 或者 ~/.zshrc 各种文件中去。
共用 npm 全局模块
使用 nvs 时,默认的 prefix 是当前激活的 node 版本的安装路径。切换 node 版本之后,之前安装全局命令模块需要重新安装,非常不方便。
建议解决方案的步骤:
- 执行 $ npm config set prefix ~/.npm-global 命令设置 prefix 到 ~/.npm-global
- 编辑 ~/.zshrc 或者 ~/.bashrc在末尾添加一行 export PATH=~/.npm-global/bin:$PATH
- 执行 source ~/.zshrc 或者 source ~/.bashrc
这样无论哪个版本的 node 安装全局模块时都会安装到 ~/.npm-global,避免在切换版本后需要重新安装。
mac下不会执行.bahsrc
在.bash_profile中加上source ~/.bashrc
或者新建.bash_profile在文件中加上export PATH=~/.npm-global/bin:$PATH
执行source ~/.bash_profile
在安装了oh_my_zsh后zsh命令找不到模块: 在 ~/.zshrc 中添加一句:
source ~/.bash_profile
然后执行source ~/.zshrc即可.
*windows下环境变量需要在path中手动添加C:\Users\用户名.npm-global*