简单说明
NVM
全称为Node Version Manager
,是一个存放在github上的工具,用于管理Node.js
版本,如果要使用Node.js
则建议先安装NVM。在github上有对nvm
详细的描述。github地址:https://github.com/creationix/nvm。
安装NVM(依赖GIT,请确保已安装GIT)
一、运行命令
$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
或者
$ wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
建议使用curl
的方式,安装后将创建目录~/.nvm
并将内容存放在这里。
命令执行时注意查看日志,如下图说明缺少环境变量配置
- 第一处提示在
~/.bashrc
,~/.bash_profile
,~/.zshrc
,~/.profile
都没有找到所需要的配置信息。 - 第二处提示我们可以将
export NVM_DIR...
这两句句命令添加到恰当的文件中,也就是上面列出的四个文件中的一个。 - 第三处的意思就是如果现在需要使用nvm,可以直接执行如下命令,然后就可以使用nvm了(这种方式在重新启动Terminal以后无法继续使用nvm)。
为了方便,我把这几句命令提取出来如下:
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
二、经过第一步的分析,需要将以上代码添加至~/.bash_profile
或者~/.profile
文件中
为了能够在重启(在Mac上则为重启Terminal)以后还能继续使用NVM,我们可以把这段配置信息添加到~/.bash_profile
或者~/.profile
文件中,经过实践测试添加到~/.bashrc
或~/.zshrc
文件中均无法在重启Terminal后使用NVM
。
具体操作如下:
$ cd ~
$ vim .bash_profile
## 将export NVM_DIR ... 粘贴到文件中
## ESC -> 键入":" -> 键入"wq" -> 回车保存
## 让配置文件里面生效
$ source .bash_profile
完成后运行nvm测试,如输出如下说明已安装成功
$ nvm
Node Version Manager
Note: <version> 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
nvm --version Print out the latest released version of nvm
nvm install [-s] <version> Download and install a <version>, [-s] from source. Uses .nvmrc if available
--reinstall-packages-from=<version> When installing, reinstall packages installed in <node|iojs|node version number>
--lts When installing, only select from LTS (long-term support) versions
--lts=<LTS name> When installing, only select from versions for a specific LTS line
nvm uninstall <version> Uninstall a version
nvm uninstall --lts Uninstall using automatic LTS (long-term support) alias `lts/*`, if available.
nvm uninstall --lts=<LTS name> Uninstall using automatic alias for provided LTS line, if available.
nvm use [--silent] <version> Modify PATH to use <version>. Uses .nvmrc if available
--lts Uses automatic LTS (long-term support) alias `lts/*`, if available.
--lts=<LTS name> Uses automatic alias for provided LTS line, if available.
nvm exec [--silent] <version> [<command>] Run <command> on <version>. Uses .nvmrc if available
--lts Uses automatic LTS (long-term support) alias `lts/*`, if available.
--lts=<LTS name> Uses automatic alias for provided LTS line, if available.
nvm run [--silent] <version> [<args>] Run `node` on <version> with <args> as arguments. Uses .nvmrc if available
--lts Uses automatic LTS (long-term support) alias `lts/*`, if available.
--lts=<LTS name> Uses automatic alias for provided LTS line, if available.
nvm current Display currently activated version
nvm ls List installed versions
nvm ls <version> List versions matching a given <version>
nvm ls-remote List remote versions available for install
--lts When listing, only show LTS (long-term support) versions
nvm ls-remote <version> List remote versions available for install, matching a given <version>
--lts When listing, only show LTS (long-term support) versions
--lts=<LTS name> When listing, only show versions for a specific LTS line
nvm version <version> Resolve the given description to a single local version
nvm version-remote <version> Resolve the given description to a single remote version
--lts When listing, only select from LTS (long-term support) versions
--lts=<LTS name> When listing, only select from versions for a specific LTS line
nvm deactivate Undo effects of `nvm` on current shell
nvm alias [<pattern>] Show all aliases beginning with <pattern>
nvm alias <name> <version> Set an alias named <name> pointing to <version>
nvm unalias <name> Deletes the alias named <name>
nvm reinstall-packages <version> Reinstall global `npm` packages contained in <version> to current version
nvm unload Unload `nvm` from shell
nvm which [<version>] Display path to installed node version. Uses .nvmrc if available
nvm cache dir Display path to the cache directory for nvm
nvm cache clear Empty cache directory for nvm
Example:
nvm install v0.10.32 Install a specific version number
nvm use 0.10 Use the latest available 0.10.x release
nvm run 0.10.32 app.js Run app.js using node v0.10.32
nvm exec 0.10.32 node app.js Run `node app.js` with the PATH pointing to node v0.10.32
nvm alias default 0.10.32 Set default node version on a shell
Note:
to remove, delete, or uninstall nvm - just remove the `$NVM_DIR` folder (usually `~/.nvm`)
卸载NVM
执行下面的命令移除nvm
内容
cd ~
rm -rf .nvm
移除掉~/.profile
, ~/.bash_profile
, ~/.zshrc
, ~/.bashrc
文件中关于nvm
的配置
网友评论