现在Node.js也有很多的版本啦,简单地使用某个版本,只需要去下载安装对应版本就可以了。
如果需要多个版本在机器上共存,并在需要时切换到相应的版本环境,这时候就需要多版本的管理工具了。
而 n, nvm就是这个有效的工具。
NVM, 即 NodeJS Version Management, 存在2个nodeJS的版本管理工具,分别是:
在MacOS里,安装使用n的方式是最简单的。
Note: 其实,n执行文件是一个bash脚本文件。感兴趣可以直接查看脚步。
工具软件n支持brew安装
% brew install n
安装成功,看看执行文件
% ls -l /usr/local/bin/n
lrwxr-xr-x 1 david admin 23 8 18 09:04 /usr/local/bin/n -> ../Cellar/n/8.0.1/bin/n
执行一下n命令:
% n -h
Usage: n [options] [COMMAND] [args]
Commands:
n Display downloaded Node.js versions and install selection
n latest Install the latest Node.js release (downloading if necessary)
n lts Install the latest LTS Node.js release (downloading if necessary)
n <version> Install Node.js <version> (downloading if necessary)
n install <version> Install Node.js <version> (downloading if necessary)
n run <version> [args ...] Execute downloaded Node.js <version> with [args ...]
n which <version> Output path for downloaded node <version>
n exec <vers> <cmd> [args...] Execute command with modified PATH, so downloaded node <version> and npm first
n rm <version ...> Remove the given downloaded version(s)
n prune Remove all downloaded versions except the installed version
n --latest Output the latest Node.js version available
n --lts Output the latest LTS Node.js version available
n ls Output downloaded versions
n ls-remote [version] Output matching versions available for download
n uninstall Remove the installed Node.js
Options:
-V, --version Output version of n
-h, --help Display help information
-p, --preserve Preserve npm and npx during install of Node.js
-q, --quiet Disable curl output. Disable log messages processing "auto" and "engine" labels.
-d, --download Download if necessary, and don't make active
-a, --arch Override system architecture
--all ls-remote displays all matches instead of last 20
--insecure Turn off certificate checking for https requests (may be needed from behind a proxy server)
--use-xz/--no-use-xz Override automatic detection of xz support and enable/disable use of xz compressed node downloads.
Aliases:
install: i
latest: current
ls: list
lsr: ls-remote
lts: stable
rm: -
run: use, as
which: bin
Versions:
Numeric version numbers can be complete or incomplete, with an optional leading 'v'.
Versions can also be specified by label, or codename,
and other downloadable releases by <remote-folder>/<version>
4.9.1, 8, v6.1 Numeric versions
lts Newest Long Term Support official release
latest, current Newest official release
auto Read version from file: .n-node-version, .node-version, .nvmrc, or package.json
engine Read version from package.json
boron, carbon Codenames for release streams
lts_latest Node.js support aliases
and nightly, rc/10 et al
按照help的信息,可以使用n工具,我们可以按照特定的nodeJS版本,切换到指定的nodeJS版本环境。
上面n是macOS内最简单的nodeJS工具了,还有nvm,它就是nodejs version management的缩写。项目的git地址: https://github.com/nvm-sh/nvm
nvm也支持各个OS平台,有爱好者分别提供了各平台的版本。
macOS内,brew并没有很好地支持nvm。
nvm 文档介绍执行下面的命令之一来进行安装
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
但因为网络稳定方面的原因,并不好用,最好先 git clone
或者下载下来再执行脚本更可靠些。
Note: 下载或clone到本地后,执行install.sh
之前,先对脚本文件 chmod +x
。感兴趣可以看一下脚本文件。
nvm需要设置NVM目录环境变量,官方推荐在. ~/.nvm
:
sudo mkdir ~/.nvm
# >>> nvm init
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
# <<< nvm init
source ~/.zshrc
sudo install.sh
在install成功后,执行一下nvm看看:
```shell
% nvm
Node Version Manager (v0.39.1)
Note: refers to any version-like string nvm understands. This includes:
- full or partial version numbers, starting with an optional "v" (0.10, v0.1.2, v1)
- default (built-in) aliases: node, stable, unstable, iojs, system
- custom aliases you define with `nvm alias foo`
Any options that produce colorized output should respect the `--no-colors` option.
Usage:
nvm --help Show this message
--no-colors Suppress colored output
nvm --version Print out the installed version of nvm
nvm install [] Download and install a . Uses .nvmrc if available and version is omitted.
The following optional arguments, if provided, must appear directly after `nvm install`:
-s Skip binary download, install from source only.
-b Skip source download, install from binary only.
--reinstall-packages-from= When installing, reinstall packages installed in
--lts When installing, only select from LTS (long-term support) versions
--lts= When installing, only select from versions for a specific LTS line
--skip-default-packages When installing, skip the default-packages file if it exists
--latest-npm After installing, attempt to upgrade to the latest working npm on the given node version
--no-progress Disable the progress bar on any downloads
--alias= After installing, set the alias specified to the version specified. (same as: nvm alias )
--default After installing, set default alias to the version specified. (same as: nvm alias default )
nvm uninstall Uninstall a version
nvm uninstall --lts Uninstall using automatic LTS (long-term support) alias `lts/*`, if available.
nvm uninstall --lts= Uninstall using automatic alias for provided LTS line, if available.
nvm use [] Modify PATH to use . Uses .nvmrc if available and version is omitted.
The following optional arguments, if provided, must appear directly after `nvm use`:
--silent Silences stdout/stderr output
--lts Uses automatic LTS (long-term support) alias `lts/*`, if available.
--lts= Uses automatic alias for provided LTS line, if available.
nvm exec [] [] Run on . Uses .nvmrc if available and version is omitted.
The following optional arguments, if provided, must appear directly after `nvm exec`:
--silent Silences stdout/stderr output
--lts Uses automatic LTS (long-term support) alias `lts/*`, if available.
--lts= Uses automatic alias for provided LTS line, if available.
nvm run [] [] Run `node` on with as arguments. Uses .nvmrc if available and version is omitted.
The following optional arguments, if provided, must appear directly after `nvm run`:
--silent Silences stdout/stderr output
--lts Uses automatic LTS (long-term support) alias `lts/*`, if available.
--lts= Uses automatic alias for provided LTS line, if available.
nvm current Display currently activated version of Node
nvm ls [] List installed versions, matching a given if provided
--no-colors Suppress colored output
--no-alias Suppress `nvm alias` output
nvm ls-remote [] List remote versions available for install, matching a given if provided
--lts When listing, only show LTS (long-term support) versions
--lts= When listing, only show versions for a specific LTS line
--no-colors Suppress colored output
nvm version Resolve the given description to a single local version
nvm version-remote Resolve the given description to a single remote version
--lts When listing, only select from LTS (long-term support) versions
--lts= When listing, only select from versions for a specific LTS line
nvm deactivate Undo effects of `nvm` on current shell
--silent Silences stdout/stderr output
nvm alias [] Show all aliases beginning with
--no-colors Suppress colored output
nvm alias Set an alias named pointing to
nvm unalias Deletes the alias named
nvm install-latest-npm Attempt to upgrade to the latest working `npm` on the current node version
nvm reinstall-packages Reinstall global `npm` packages contained in to current version
nvm unload Unload `nvm` from shell
nvm which [current | ] Display path to installed node version. Uses .nvmrc if available and version is omitted.
--silent Silences stdout/stderr output when a version is omitted
nvm cache dir Display path to the cache directory for nvm
nvm cache clear Empty cache directory for nvm
nvm set-colors [] Set five text colors using format "yMeBg". Available when supported.
Initial colors are:
g b y r e
Color codes:
r/R = red / bold red
g/G = green / bold green
b/B = blue / bold blue
c/C = cyan / bold cyan
m/M = magenta / bold magenta
y/Y = yellow / bold yellow
k/K = black / bold black
e/W = light grey / white
Example:
nvm install 8.0.0 Install a specific version number
nvm use 8.0 Use the latest available 8.0.x release
nvm run 6.10.3 app.js Run app.js using node 6.10.3
nvm exec 4.8.3 node app.js Run `node app.js` with the PATH pointing to node 4.8.3
nvm alias default 8.1.0 Set default node version on a shell
nvm alias default node Always default to the latest available node version on a shell
nvm install node Install the latest available version
nvm use node Use the latest version
nvm install --lts Install the latest LTS version
nvm use --lts Use the latest LTS version
nvm set-colors cgYmW Set text colors to cyan, green, bold yellow, magenta, and white
Note:
to remove, delete, or uninstall nvm - just remove the `$NVM_DIR` folder (usually `~/.nvm`)
好了, 可以使用nvm来按照,管理,切换nodeJS各个版本。
执行:
% nvm ls-remote --lts
列出来所有的lts版本,当前lts的nodeJS是16.17版本,安装一下:
% nvm install 16
安装v16最新的版本, 在安装另一个版本:
% nvm install 14
Downloading and installing node v14.20.0...
Downloading https://nodejs.org/dist/v14.20.0/node-v14.20.0-darwin-x64.tar.xz...
############################################################################################################################################################################################################# 100.0%
Computing checksum with shasum -a 256
Checksums matched!
Now using node v14.20.0 (npm v6.14.17)
列出安装的版本信息:
% nvm list
-> v14.20.0
v16.17.0
system
default -> 16.17 (-> v16.17.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v16.17.0) (default)
stable -> 16.17 (-> v16.17.0) (default)
lts/* -> lts/gallium (-> v16.17.0)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.20.0
lts/gallium -> v16.17.0
%
% node
Welcome to Node.js v14.20.0.
Type ".help" for more information.
>
当前版本Node.js v14.20, 切换到v16版本
% nvm use 16
Now using node v16.17.0 (npm v8.15.0)
% node
Welcome to Node.js v16.17.0.
Type ".help" for more information.
>
现在你可以有效地管理使用多版本的Node.js了。