Node.js® 是一个基于 Chrome V8 引擎 的 JavaScript 运行时。
中文官方网站:https://nodejs.org/zh-cn/
nvm
nvm is a version manager for node.js, designed to be installed per-user, and invoked per-shell.
nvm
works on any POSIX-compliant shell (sh, dash, ksh, zsh, bash), in particular on these platforms: unix, macOS, and windows WSL.
nvm是node.js的版本管理工具,它可以帮助我们在不同的Node版本中切换,以适应不同项目的需要。
下载并安装 nvm
使用 cURL 或 Wget 命令进行下载并安装
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash
或者
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash
macos:~ xxx$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 13527 100 13527 0 0 25331 0 --:--:-- --:--:-- --:--:-- 25331
=> nvm is already installed in /Users/xxx/.nvm, trying to update using git
=> => Compressing and cleaning up git repository
=> nvm source string already in /Users/xxx/.bash_profile
=> bash_completion source string already in /Users/xxx/.bash_profile
=> Close and reopen your terminal to start using nvm or run the following to use it now:
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
安装完成后,输入 nvm 测试是否成功
macos:~ xxx$ nvm
Node Version Manager (v0.35.2)
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
nvm --version Print out the installed version of nvm
nvm install [-s] Download and install a , [-s] from source. Uses .nvmrc if available
--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
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 [--silent] Modify PATH to use . Uses .nvmrc if available
--lts Uses automatic LTS (long-term support) alias `lts/*`, if available.
--lts= Uses automatic alias for provided LTS line, if available.
nvm exec [--silent] [] Run on . Uses .nvmrc if available
--lts Uses automatic LTS (long-term support) alias `lts/*`, if available.
--lts= Uses automatic alias for provided LTS line, if available.
nvm run [--silent] [] Run `node` on with as arguments. Uses .nvmrc if available
--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
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
nvm cache dir Display path to the cache directory for nvm
nvm cache clear Empty cache directory for nvm
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
Note:
to remove, delete, or uninstall nvm - just remove the `$NVM_DIR` folder (usually `~/.nvm`)
错误1
curl: (35) LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to raw.githubusercontent.com:443
解决办法:移除 git 代理后,再重新安装
git config --global --unset http.proxy
错误2
error: RPC failed; curl 18 transfer closed with outstanding read data remaining
fatal: the remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed
Failed to clone nvm repo. Please report this!
解决办法:这是因为 postBuffer 的默认值太小,造成缓存区溢出,调整 postBuffer 的默认值为100M(根据需要设置),再重新安装
git config --global http.postBuffer 104857600
错误3
=> Compressing and cleaning up git repository
=> Profile not found. Tried ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile.
=> Create one of them and run this script again
OR
=> Append the following lines to the correct file yourself:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm`
解决办法:这是因为缺少配置文件,错误信息已经告诉我们处理方法了,在当前用户的根目录下,创建一个 .bash_profile 文件 touch .bash_profile
,再重新安装。
重新安装后,查看 .bash_profile 文件,内容如下:
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 下载源
默认的下载源很慢,将其更改为淘宝的下载源,检查当前用户的根目录下是否有 ~/.zshrc 文件,如果没有就创建一个 touch .zshrc
,并在 .zshrc 文件中写入以下内容:
export NVM_NODEJS_ORG_MIRROR=http://npm.taobao.org/mirrors/node
export NVM_IOJS_ORG_MIRROR=http://npm.taobao.org/mirrors/iojs
保存后,运行 source ~/.zshrc
更新环境变量,这样再用 nvm 下载 node 就快多了。
nvm 相关操作
nvm ls-remote
列出远程服务器上的所有版本
nvm list
或 $ nvm ls
查看已安装的版本
nvm current
查看当前的版本
nvm alias default
指定一个默认的版本
nvm install
安装指定的版本
nvm use
切换不同的版本
nvm uninstall
删除指定的版本
Node.js 的版本号
Node.js 的版本命名规则遵循 Semantic Versioning(语意版本号)规范 ,版本号分为MAJOR.MINOR.PATCH(主版本号.次版本号.补丁版本号)。
- 当你修改了 API,使其(与之前版本)不兼容时,递增 MAJOR;
- 当你用向后兼容的方式加了些功能时,递增 MINOR;
- 当你用向后兼容的方式解决了几个 Bug 时,递增 PATCH;
选择要安装的版本时,尽量选择 LTS(Long Term Support)版,Node.js 每年都会出一个 LTS 版本,版本代号从元素周期表中挑选,v4 的代号是 Argon(氩),v6 的代号是 Boron(硼)... 目前 v12 的代号是 Erbium(铒)。