install nvm
see: https://github.com/nvm-sh/nvm
- 先在系统安装一个 system 版本的 nodejs 软件
- 执行命令
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
安装 nvm - 在
~/.bash_profile
,~/.zshrc
,~/.profile
, or~/.bashrc
添加 配置文件
1 | export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")" |
使用 nvm 的规范
- nvm use version 之后, 使用 npm 安装的全局包会优先于 使用 yarn pnpm 安装的全局包 (use 之后安装的无效仍是 yarn 版在前, 需要重新 source ~/.zshrc)
- npm 安装的全局包与 node version 关联, 但是 yarn pnpm 安装的全局包, 跨 node 版本使用 => 如果有包是对 node 版本有兼容性需求, 必须使用 npm 方式全局安装
总结:
npm 全局包只在当前 version 有效, 每个 version 要用都需要单独安装, 但是兼容性好
yarn 全局包跨 node 版本使用, 一次安装全版本可用, 但是有兼容性需求的不建议使用