Node.js 安装与版本管理
简单的说 Node.js 就是运行在服务端的 JavaScript。
- Node.js 是一个基于 Chrome JavaScript 运行时建立的一个平台。
- Node.js 是一个事件驱动 I/O 服务端 JavaScript 环境,基于 Google 的 V8 引擎,V8 引擎执行 Javascript 的速度非常快,性能非常好。
安装 nvm
推荐先安装 nvm,再通过 nvm 去安装 Node.js:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
Running either of the above commands downloads a script and runs it. The script clones the nvm repository to ~/.nvm
, and attempts to add the source lines from the snippet below to the correct profile file (~/.bash_profile
, ~/.zshrc
, ~/.profile
, or ~/.bashrc
).
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
Calling nvm use automatically in a directory with a .nvmrc
file.
Put this into your $HOME/.zshrc
to call nvm use automatically whenever you enter a directory that contains an .nvmrc
file with a string telling nvm which node to use
:
# place this after nvm initialization!
autoload -U add-zsh-hook
load-nvmrc() {
local nvmrc_path="$(nvm_find_nvmrc)"
if [ -n "$nvmrc_path" ]; then
local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
if [ "$nvmrc_node_version" = "N/A" ]; then
nvm install
elif [ "$nvmrc_node_version" != "$(nvm version)" ]; then
nvm use
fi
elif [ -n "$(PWD=$OLDPWD nvm_find_nvmrc)" ] && [ "$(nvm version)" != "$(nvm version default)" ]; then
echo "Reverting to nvm default version"
nvm use default
fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc
安装 Node.js
To download, compile, and install the latest release of node, do this:
nvm install node # "node" is an alias for the latest version
nvm install 14.7.0 # or 16.3.0, 18, etc
The first version installed becomes the default. New shells will start with the default version of node.
nvm alias default 16.14.2 # nvm set default node.js version 16.14.2
You can list available versions using ls-remote:
nvm ls-remote