文章

🎨 canvas 安装慢、安装失败的解决方法

canvas 安装过程中的常见问题及解决方案

📦 canvas 的安装过程

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

⚙️ 命令行指定 canvas 的镜像

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

📄 修改全局的配置 (~/.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 [email protected] 的预编译共享库并未同步到镜像, 如安装失败可切换到 [email protected] 使用
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
本文由作者按照 CC BY 4.0 进行授权