• 欢迎访问少将全栈,学会感恩,乐于付出,珍惜缘份,成就彼此、推荐使用最新版火狐浏览器和Chrome浏览器访问本网站。
  • 吐槽,投稿,删稿,交个朋友,商务沟通v:ai_draw
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏少将全栈吧

解决npm安装node环境中canvas安装缓慢或失败的有效方法

开发笔记 admin 12个月前 (05-02) 3092次浏览 已收录 0个评论 扫描二维码

如果你使用node模块编程,你可能会遇到一些跨平台问题。

有些node模块使用原生C/C++代码,这些模块要想跨平台就需要源码构建。

比如canvas和SQLite3安装过程中的主要问题。

下面,让我们来探讨一下canvas的安装过程。

首先,要在npm仓库中下载canvas包,然后执行canvas的package.json中的install命令(node-pre-gyp install –fallback-to-build)。

接着,node-pre-gyp将下载已编译好的macOS、Linux和Windows二进制文件,如果预构建的二进制文件不存在或不可用,它将从源代码构建。

最后,node-gyp将会编译为当前平台可用的node模块。

但是为什么有些用户会觉得canvas的安装过程缓慢或失败呢?从安装过程来看,npm的官网地址:https://www.npmjs.com/package/canvas和canvas二进制文件托管在https://github.com/node-gfx/node-canvas-prebuilt,如果你的网络运营商给力,你将大概率安装成功,否则你将会得到read ECONNRESET的提示。那么,我们应该如何解决canvas安装过程中的慢、失败问题呢?

npm的官网介绍如下:

By default, binaries for macOS, Linux and Windows will be downloaded. If you want to build from source, use npm install --build-from-source and see the Compiling section below.
默认情况下,将下载适用于 macOS、Linux 和 Windows 的二进制文件。如果您想从源代码构建,请使用 npm install --build-from-source 并查看下面的编译部分。

The minimum version of Node.js required is 6.0.0.
所需的最低 Node.js 版本为 6.0.0。

Compiling

If you don’t have a supported OS or processor architecture, or you use --build-from-source, the module will be compiled on your system. This requires several dependencies, including Cairo and Pango.
如果您没有受支持的操作系统或处理器体系结构,或者您使用 --build-from-source ,则该模块将在您的系统上编译。这需要几个依赖项,包括 Cairo 和 Pango。

For detailed installation information, see the wiki. One-line installation instructions for common OSes are below. Note that libgif/giflib, librsvg and libjpeg are optional and only required if you need GIF, SVG and JPEG support, respectively. Cairo v1.10.0 or later is required.
有关详细的安装信息,请参阅 wiki。常见操作系统的单行安装说明如下。请注意,libgif/giflib、librsvg 和 libjpeg 是可选的,仅当您分别需要 GIF、SVG 和 JPEG 支持时才需要。需要 Cairo v1.10.0 或更高版本。

OSCommand
OS XUsing Homebrew: 使用自制软件:
brew install pkg-config cairo pango libpng jpeg giflib librsvg pixman
Ubuntusudo apt-get install build-essential libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev librsvg2-dev
Fedorasudo yum install gcc-c++ cairo-devel pango-devel libjpeg-turbo-devel giflib-devel
Solarispkgin install cairo pango pkg-config xproto renderproto kbproto xextproto
OpenBSDdoas pkg_add cairo pango png jpeg giflib
WindowsSee the wiki
见维基
OthersSee the wiki
见维基

Mac OS X v10.11+: If you have recently updated to Mac OS X v10.11+ and are experiencing trouble when compiling, run the following command: xcode-select --install. Read more about the problem on Stack Overflow. If you have xcode 10.0 or higher installed, in order to build from source you need NPM 6.4.1 or higher.
Mac OS X v10.11+:如果您最近更新到 Mac OS X v10.11+ 并且在编译时遇到问题,请运行以下命令: xcode-select --install 。在 Stack Overflow 上阅读有关该问题的更多信息。如果你安装了 xcode 10.0 或更高版本,为了从源代码构建你需要 NPM 6.4.1 或更高版本。

 

github相关的服务在国内速度不理想,我们可以使用国内镜像来加速。我们可以通过npm的config参数来指定canvas的二进制文件下载镜像。

例如(主要通过这个方法解决)npm install canvas --canvas_binary_host_mirror=https://registry.npmmirror.com/-/binary/canvas

解决npm安装node环境中canvas安装缓慢或失败的有效方法

目前的问题,先安装了依赖 brew install pkg-config cairo pango libpng jpeg giflib librsvg pixman 再主要通过指定源 npm install canvas --canvas_binary_host_mirror=https://registry.npmmirror.com/-/binary/canvas 解决

当然,我们不希望在命令行中添加参数,因为它不便于维护。那么我们应该如何做呢?

其实,我们可以在.npmrc文件中设置一个或多个模块的二进制文件下载镜像地址。

一个好的长期维护的.npmrc文件可以让我们的任何项目安装过程更加轻松愉快。

我的.npmrc文件设置如下:

registry=https://registry.npmmirror.com
disturl=https://registry.npmmirror.com/-/binary/node/
sass_binary_site=https://registry.npmmirror.com/-/binary/node-sass
canvas_binary_host_mirror=https://registry.npmmirror.com/-/binary/canvas

其中,registry.npmmirror.com是一个国内镜像地址,目前这个地址被认为是较为合适的一个地址。

建议大家使用这个地址来解决canvas安装过程中的慢、失败问题。

解决canvas安装过程中的慢、失败问题并不难,只需要指定源命令或者通过.npmrc配置文件使用国内镜像即可。

如果你安装出现问题,请尝试使用上述方法来解决。

 

喜欢 (2)
[🍬谢谢你请我吃糖果🍬🍬~]
分享 (0)
关于作者:
少将,关注Web全栈开发、项目管理,持续不断的学习、努力成为一个更棒的开发,做最好的自己,让世界因你不同。
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址