mac 安装 node.js 的 canvas

安装指南在这,但是有坑。。

https://github.com/Automattic/node-canvas/wiki/Installation---OSX

1、安装pkg-config

$ curl http://pkgconfig.freedesktop.org/releases/pkg-config-0.28.tar.gz -o pkgconfig.tgz
$ tar -zxf pkgconfig.tgz && cd pkg-config-0.28
$ ./configure && make install

问题1:安装包文件夹别放在名字有空格的文件夹里,读不出来

问题2:install: /usr/local/bin/pkg-config: Permission denied

   这里不是说用sudo 就行了,是说/usr/local/bin/有问题

   运行 sudo chmod 777 /usr/local/bin/

   ps:

    $ ./configure && make install

   这句话最好分开写,记得加sudo

    $ ./configure

   这句话没有用的时候用:

    sudo ./configure --with-internal-glib && make install

然后出现一批make[5]: Nothing to be done for [***]就成功了

2、安装pixman

   按照他的来

    $ curl http://www.cairographics.org/releases/pixman-0.30.0.tar.gz -o pixman.tar.gz
    $ tar -zxf pixman.tar.gz && cd pixman-0.30.0/
    $ ./configure --prefix=/usr/local --disable-dependency-tracking
    $ make install

    安装0.30.0,是个坑爹的选择,出现了clang 编译错误,好像解决就要安装homebrew,但是仍然完全不知道如何解决。。

    冒险安装了最新版本0.34.0 问题解决,目前没有不良反应。。

3、安装cairo

  $ curl http://cairographics.org/releases/cairo-1.12.18.tar.xz -o cairo.tar.xz
  $ tar -xf cairo.tar.xz && cd cairo-1.12.18
  $ ./configure --prefix=/usr/local --disable-dependency-tracking
  $ make install

   考虑到pixman用的最新版本,这个我也下了最新的cairo-1.14.6

  问题1:configure: error: recommended PNG functions feature could not be enabled

  这个官网上有解决方案:http://ethan.tira-thompson.com/Mac_OS_X_Ports.html 安装这个库

  为了保险运行了下面两句话:

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

   然后sudo make install成功

4、安装canvas

  到要安装canvas的文件夹npm ,失败:

> canvas@1.3.10 install******/node_modules/canvas
> node-gyp rebuild

SOLINK_MODULE(target) Release/canvas-postbuild.node
CXX(target) Release/obj.target/canvas/src/Canvas.o
In file included from ../src/Canvas.cc:20:
../src/JPEGStream.h:10:10: fatal error: 'jpeglib.h' file not found
#include <jpeglib.h>
^
1 error generated.
make: *** [Release/obj.target/canvas/src/Canvas.o] Error 1
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:270:23)
gyp ERR! stack at emitTwo (events.js:87:13)
gyp ERR! stack at ChildProcess.emit (events.js:172:7)
gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:200:12)
gyp ERR! System Darwin 15.3.0
gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /Users/fengyujia/Documents/My Code/zc_core/node_modules/canvas
gyp ERR! node -v v5.3.0
gyp ERR! node-gyp -v v3.0.3
gyp ERR! not ok
npm ERR! Darwin 15.3.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "canvas"
npm ERR! node v5.3.0
npm ERR! npm v3.3.12
npm ERR! code ELIFECYCLE

npm ERR! canvas@1.3.10 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the canvas@1.3.10 install script 'node-gyp rebuild'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the canvas package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node-gyp rebuild
npm ERR! You can get their info via:
npm ERR! npm owner ls canvas
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR! ******/npm-debug.log

解决办法看这:https://github.com/Automattic/node-canvas/issues/225

按着他们的讨论一路执行下去,最后的那句

xcode-select --install

有效(不知道前面的命令有没有影响)

安装完这个后就可以npm install canvas了


    

原文地址:https://www.cnblogs.com/JohannaFeng/p/5229552.html