Node 版本推荐用最新的lts版本(目前12.x), 尝鲜能够用current版本(通常比稳定版超前一个大版本)node
NodeJS 官网下载安装lts或者current版本:nodejs 官网
linux
有时候须要用到不一样Node的版本运行不一样的项目,git
单版本就很局限了,因此社区也出了多版本管理的工具
github
nvm一开始只为linux和macos实现,由于是用shell脚本写的,
后续流行起来后,就开始有周边了,包括兼容windows的衍生库
shell
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
# 或者
$ wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
# 上面的执行成功后,还须要写入环境变量,具体改动你的shell配置文件(用户根目录下的)
# bash -> .bashrc
# zsh -> .zshrc
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
复制代码
直接下载 nvm-windows安装包安装便可。macos
$ nvm install 10.15.0 # 下载编译和安装指定版本node
$ nvm use 10.15.0 # 切换(使用)指定版本node
$ nvm alias default 10.15.0 # 设置shell默认版本
复制代码
更多 nvm 用法查看文档 nvm 文档。
npm
nvs默认支持全平台,用node写的 windows
# 声明一个临时变量
export NVS_HOME="$HOME/.nvs"
# 克隆仓库
git clone https://github.com/jasongin/nvs "$NVS_HOME"
# 执行脚本安装
. "$NVS_HOME/nvs.sh" install
复制代码
brew
choco install nvs
操做也是很直观,跟nvm同样很直白bash
$ nvs --help
NVS (Node Version Switcher) usage
nvs help <command> Get detailed help for a command
nvs install Initialize your profile for using NVS
nvs --version Display the NVS tool version
nvs menu Launch an interactive menu
nvs add <version> Download and extract a node version
nvs rm <version> Remove a node version
nvs migrate <fromver> [tover] Migrate global modules
nvs upgrade [fromver] Upgrade to latest patch of major version
nvs use [version] Use a node version in the current shell
nvs auto [on/off] Automatically switch based on cwd
nvs run <ver> <js> [args...] Run a script using a node version
nvs exec <ver> <exe> [args...] Run an executable using a node version
nvs which [version] Show the path to a node version binary
nvs ls [filter] List local node versions
nvs ls-remote [filter] List node versions available to download
nvs link [version] Link a version as the default
nvs unlink [version] Remove links to a default version
nvs alias [name] [value] Set or recall aliases for versions
nvs remote [name] [uri] Set or recall download base URIs
A version string consists of a semantic version number or version label
("lts" or "latest"), optionally preceeded by a remote name, optionally
followed by an architecture, separated by slashes.
Examples: "lts", "4.6.0", "6.3.1/x86", "node/6.7.0/x64"
Aliases may also be used anywhere in place of a version string.
复制代码
$ nvs add lts # 安装最新的LTS
$ nvs use lts # 切换指定的 node 版本
$ nvs link lts# 配置为默认版本,设置shell默认版本
复制代码
公司内部搭建了npm 私有仓库,仓库内包含运行项目必要的依赖;
常规的源切换,只能用npm config
去设置局部或者全局的源,步骤和操做上有点繁琐。
针对这种状况社区也出了方便维护和快速切换的工具:nrm
curl
全局安装nrm
npm install -g nrm
复制代码
添加一个新的 npm 源,
nrm add h3npm http://xxxxx/repository/npm-all/ # 公司信息,脱敏
复制代码
nrm use h3npm
复制代码
运行nrm ls
命令查看当前设置的 npm 源,*
表明当前 npm 源
$ nrm ls
npm -------- https://registry.npmjs.org/ # npm官方源
yarn ------- https://registry.yarnpkg.com/ # yarn的官方源
cnpm ------- http://r.cnpmjs.org/ # cnpm 源
taobao ----- https://registry.npm.taobao.org/ # 淘宝官方镜像源
nj --------- https://registry.nodejitsu.com/ # 国外的官方镜像源
* h3npm ------ http://xxxxx/repository/npm-all/ # 公司内部官方镜像源(涵盖私有库)
h3-authine - http://xxxxx/repository/npm-authine/ # 私有库源
复制代码