00. Node.js 的安装

linux 下安装

直接使用命令行进行安装,但版本往往很旧。

下载完安装包会提示:

This package has installed:

• Node.js v10.15.3 to /usr/local/bin/node
• npm v6.4.1 to /usr/local/bin/npm

Make sure that /usr/local/bin is in your $PATH.

当然也可以去 Node 官网下载已编译好的 Linux 二进制文件 .tar.xz 格式

1. 下载并解压

1
2
3
4
5
# 下载 Node.js 安装包
wget https://npm.taobao.org/mirrors/node/v12.4.0/node-v12.4.0-linux-x64.tar.xz

# 解压安装包并重命名
tar -xvf node-v12.4.0-linux-x64.tar.xz && mv node-v12.4.0-linux-x64/ /usr/local/node

2. 配置环境变量 或 建立软连接

1
2
3
# 配置环境变量
echo "export PATH=$PATH:/usr/local/node/bin" >> /etc/profile
source /etc/profile

另外一种方式是可以使用 ln 命令来设置软连接(必须是完整绝对路径才行)

1
2
ln -s /home/zhangsan/node-v10.16.0-linux-x64/bin/npm /usr/local/bin/
ln -s /home/zhangsan/node-v10.16.0-linux-x64/bin/node /usr/local/bin/

Windows 上安装

可以选择 Windows 安装包(.msi) 或者 zip 包。

Mac OS 上安装

你可以通过以下两种方式在 Mac OS 上来安装 node:

  1. 在官方下载网站下载 pkg 安装包,直接点击安装即可。
  2. 使用 brew 命令来安装:brew install node

使用 nvm 安装 node【推荐】

nvm 是一种流行的运行 Node.js 的方式。它可以帮助开发者轻松切换 Node.js 版本,也可以安装新版本用以尝试并且当出现问题时轻松地回滚。

linux 和 mac 系统

nvm-sh/nvm: Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versions

要安装或更新 nvm,应该运行安装脚本。为此,您可以手动下载并运行脚本,或者使用以下 cURL 或 Wget 命令:

1
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash

1
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash

国内镜像加速

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 临时设置镜像环境变量
export NVM_NODEJS_ORG_MIRROR=https://npmmirror.com/mirrors/node
export NVM_IOJS_ORG_MIRROR=https://npmmirror.com/mirrors/iojs

# 使用 Gitee 镜像安装
curl -o- https://gitee.com/mirrors/nvm/raw/v0.40.3/install.sh | bash

# 然后安装
nvm install 24
# 激活使用
nvm use 24
nvm alias default 24

# 验证配置成功,先查看当前镜像源
echo $NVM_NODEJS_ORG_MIRROR

# 查看 node 版本
node -v # 应显示 v24.x.x

# 查看 npm 源(建议也换成国内)
npm config get registry # 默认是 https://registry.npmjs.org/
npm config set registry https://registry.npmmirror.com

windows 系统

windows 用户可以使用 nvm-windows

coreybutler/nvm-windows: A node.js version management utility for Windows. Ironically written in Go.
https://github.com/coreybutler/nvm-windows

我这里选择的是 nvm-noinstall 绿色版。去 https://github.com/coreybutler/nvm-windows/releases 找到最新版本并下载

以 1.1.11 为例,直接下载 https://github.com/coreybutler/nvm-windows/releases/download/1.1.11/nvm-noinstall.zip 并解压。

双击 install.cmd 或者以管理员身份运行,在选择路径中可以输入任意路径,建议在此目录下新建一个文件夹,取名随意,例如 mynode

可选配置国内镜像

1
2
3
4
# 设置 npm_mirror:
nvm npm_mirror https://npmmirror.com/mirrors/npm/
# 设置 node_mirror:
nvm node_mirror https://npmmirror.com/mirrors/node/

nvm 的使用

首先可通过 nvm list available 查找版本号

使用 nvm install <version> 进行特定版本的安装,例如 nvm install 14.21.3

现在可以列出已安装的 Node 版本:nvm ls

在安装所需的 Node.js 版本后,可通过输入 nvm use <version> 来选择要使用的版本

1
2
nvm use 24
nvm use 24

docker 下安装 Node

node Tags | Docker Hub

简单启动

1
docker run -itd --name my-node node

复杂启动

1
2
3
4
docker run -it --name my-node ^
-v D:/alee/docker/node/myapp:/usr/src/myapp ^
-w /usr/src/myapp ^
node

若加上 rm 命令,则在自动移除 container 在退出的时候。

1
--rm  Automatically remove the container when it exits

构建后可直接从容器进行启动

1
docker start my-node

进入容器

1
docker exec -it my-node bash

验证

1
2
node -v
npm -v

参考

nvm-sh/nvm: Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versions