Node版本及源管理

3,831 阅读2分钟

前言

Node 版本推荐用最新的lts版本(目前12.x), 尝鲜可以用current版本(一般比稳定版超前一个大版本)

Node单版本

NodeJS 官网下载安装lts或者current版本:nodejs 官网

Node多版本

有时候需要用到不同Node的版本运行不同的项目,

单版本就很局限了,所以社区也出了多版本管理的工具

nvm

nvm一开始只为linux和macos实现,因为是用shell脚本写的,
后续流行起来后,就开始有周边了,包括兼容windows的衍生库

Linux/macOS上安装

$ 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

windows上安装

直接下载 nvm-windows安装包安装即可。

$ nvm install 10.15.0 # 下载编译和安装指定版本node
$ nvm use 10.15.0 # 切换(使用)指定版本node
$ nvm alias default 10.15.0 # 设置shell默认版本


更多 nvm 用法查看文档 nvm 文档

nvs

nvs默认支持全平台,用node写的

Linux/macOS上安装

# 声明一个临时变量
export NVS_HOME="$HOME/.nvs"

# 克隆仓库
git clone https://github.com/jasongin/nvs "$NVS_HOME"

# 执行脚本安装
. "$NVS_HOME/nvs.sh" install

windows上安装

  • 安装chocolatey,类似mac下的brew
  • choco install nvs


操作也是很直观,跟nvm一样很直白

$ 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默认版本

Node源管理


公司内部搭建了npm 私有仓库,仓库内包含运行项目必要的依赖;

常规的源切换,只能用npm config去设置局部或者全局的源,步骤和操作上有点繁琐。

针对这种情况社区也出了方便维护和快速切换的工具:nrm

nrm 安装

全局安装nrm

npm install -g nrm

添加内部私仓地址

添加一个新的 npm 源,

nrm add h3npm http://xxxxx/repository/npm-all/  # 公司信息,脱敏

切换 npm 源到私仓

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/ # 私有库源