npm install canvas简明指南

廖雪峰 / 文章 / ... / Reads: 61166 Edit

安装环境:Mac OSX Mavericks

要使用Node.js的canvas库,直接通过npm命令安装,通常,会得到一个编译错误:

$ npm install canvas
...
> node-gyp rebuild
...
No package 'cairo' found
...
npm ERR! not ok code 0

原因是依赖包cairo没有找到。

而cairo可以通过homebrew安装,但是,安装时又会有一个依赖错误:

$ brew install cairo
cairo: Unsatisfied dependency: XQuartz 
Homebrew does not package XQuartz. Installers may be found at:
  https://xquartz.macosforge.org
Error: An unsatisfied requirement failed this build.

根据错误提示,原因是没有安装XQuartz。由于Apple从OSX 10.5开始移除了X11的支持,为了继续运行X11程序,Apple搞了一个开源项目XQuartz来继续提供X11的支持。从官方网站https://xquartz.macosforge.org下载,安装即可。

装完XQuartz,再次运行:

$ brew install cairo

顺利安装成功!

再次尝试安装canvas,可能又会遇到以下错误:

$ npm install canvas
...
Package xcb-shm was not found in the pkg-config search path.
Perhaps you should add the directory containing `xcb-shm.pc'
to the PKG_CONFIG_PATH environment variable
Package 'xcb-shm', required by 'cairo', not found
...

原因是环境变量PKG_CONFIG_PATH没有正确设置。把它设置成如下:

$ export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/opt/X11/lib/pkgconfig

再次安装npm install canvas,成功!在Node.js中测试,成功:

$ node
> var Canvas = require('canvas');
undefined

由于Canvas依赖较多,故记录以便以后查阅。

Comments

Make a comment

Author: 廖雪峰

Publish at: ...

关注公众号不定期领红包:

加入知识星球社群:

关注微博获取实时动态: