摘自:https://blog.csdn.net/EverRose/article/details/122846767

Node.js 是跨平台的,那么对于任何的 Node 模块理论也是应该是跨平台的。然而,有些 Node 模块直接或间接使用原生 C/C++ 代码,这些东西要跨平台,就需要使用源码根据实际的操作平台环境进行原生模块编译。这也是 Canvas、SQLite3 等模块安装的折磨之源。

Canvas 的安装过程

  1. npm 下载在仓库中的 Canvas 包
  2. 执行 Canvas 的 package.json 中的 install 命令(node-pre-gyp install --fallback-to-build
  3. node-pre-gyp 下载 Canvas 已编译好的 macOS、Linux 和 Windows 的二进制文件,如果预构建的二进制文件不存在或不可用,则回退到从源代码构建
  4. node-gyp 编译为当前平台可用的 Node 模块

为什么安装慢,还容易失败

从安装过程可以发现,步骤 3 和步骤 4 是核心问题区。Canvas 的二进制文件托管在 https://github.com/Automattic/node-canvas/releases/download/,下载服务由 https://objects.githubusercontent.com/ 提供。如果你的网络运营商给力,你将会大概率成功,否则你将会得到 read ECONNRESET 的提示。而使用源码构建非常繁琐,对系统 OS 存在诸多限制,开发环境和运行环境的系统 OS 不一致也会大大增加我们的工作量且导致一些不可预料且难以捉摸的问题。

如何解决 Canvas 安装过程中的慢、失败问题

既然 GitHub 相关的服务在国内速度不理想,我们自然可以使用国内镜像的方式加速。那我们如何对 Canvas 的二进制文件指定下载镜像呢?我们需要给 npm 提供 config 参数 --{module_name}_binary_host_mirror 可以通过镜像下载二进制文件。

例如:

1
npm install canvas --canvas_binary_host_mirror=https://registry.npmmirror.com/-/binary/canvas

当然我们不希望在命令行中添加参数,因为它不便于维护。我们更希望能在配置文件中固化,我们可以在 .npmrc(npm 的配置文件)中设置一个或多个模块的二进制文件下载镜像地址,一个好的长期维护的 .npmrc 文件可以让我们的任何项目安装过程更加轻松愉悦。

我的 .npmrc 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
registry=https://registry.npmmirror.com

disturl=https://registry.npmmirror.com/-/binary/node/
# node-sass 预编译二进制文件下载地址
sass_binary_site=https://registry.npmmirror.com/-/binary/node-sass
# sharp 预编译共享库, 截止 2022-09-20 sharp@0.31.0 的预编译共享库并未同步到镜像, 如安装失败可切换到 sharp@0.30.7 使用
sharp_libvips_binary_host=https://registry.npmmirror.com/-/binary/sharp-libvips
python_mirror=https://registry.npmmirror.com/-/binary/python/
electron_mirror=https://registry.npmmirror.com/-/binary/electron/
electron_builder_binaries_mirror=https://registry.npmmirror.com/-/binary/electron-builder-binaries/
# 无特殊配置参考 {pkg-name}_binary_host_mirror={mirror}
canvas_binary_host_mirror=https://registry.npmmirror.com/-/binary/canvas
node_sqlite3_binary_host_mirror=https://registry.npmmirror.com/-/binary/sqlite3
better_sqlite3_binary_host_mirror=https://registry.npmmirror.com/-/binary/better-sqlite3

为什么使用 https://registry.npmmirror.com

目前网上搜索到的 Canvas 二进制文件镜像配置均是如下,此镜像在年前的时候确实可以正常安装,但是年后我在执行 npm i 构建项目时却执行失败。浏览器访问镜像地址,镜像地址下已没有任何文件。

1
canvas_binary_host_mirror=https://npm.taobao.org/mirrors/node-canvas-prebuilt/

在知乎发现淘宝 NPM 镜像站切换新域名啦的文章,老 http://npm.taobao.orghttp://registry.npm.taobao.org 域名将于 2022 年 05 月 31 日零时起停止服务,由 https://registry.npmmirror.com 域名继续提供服务。目前两个老的域名均会 301 重定向到新的镜像域名,但强烈建议大家在停服之前切换到新的镜像域名。