nodeのcanvasでのエラー(Canvas is not a constructor)解消方法
nodeのcanvasでのエラー(Canvas is not a constructor)解消方法
1.概要
nodeでcanvasを動かそうとして、テストとしてネットに流れているサンプルを実行したところ「Canvas is not a constructor」のエラーで動作しなかった。動いた実績のあるサンプルと推測できるのでインストールの問題と思って色々やったが動作しなかった。エラーメッセージを頼りにネット検索したら参照URLに辿り着いた。
そこにあるようにサンプルのソースを変更したら動作した。どこかでcanvasの仕様が変更になり今まで動いていたプログラムが動作しなくなっていたらしい。
2.ソース修正方法
参照URLの内容のとおりだが以下のような変更をする。
new Canvas.createCanvas
→
Canvas.createCanvas
new Canvas();
→
Canvas.createCanvas();
#つまりnewを使用しないようにする。
3.本家のサンプルを動作させる
復習を兼ねてインストールから動作完了までを記載する:
git clone https://github.com/Automattic/node-canvas.git
cp -a node-canvas/examples samples
cd samples
sudo apt-get install libcairo2-dev
npm install canvas
別のディレクトリにコピーしたので動かしたいjsを以下のように修正する(例):
var Canvas = require('..')
→
var Canvas = require('canvas')
img('examples/images/squid.png')
→
img('./images/squid.png')
img('examples/images/squid.png')
→
img('./images/squid.png')
#言うまでもないが、本家サンプルは、もともとエラーは出ないので、それに対する修正はない。
4.参照URL
https://github.com/Automattic/node-canvas/issues/997
Canvas is not a constructor
以上
| 固定リンク
「Termux」カテゴリの記事
- nodeのcanvasでのエラー(Canvas is not a constructor)解消方法(2019.01.06)
- Termux-ubuntu Unixbench(2018.12.01)
- prootでubuntuを動かす(2018.11.17)
- Termux-Ubuntu Trouble Shoot#1(groups: cannot find name for group ID xxxxx)(2018.11.11)
- LinuxBrew install memo(2018.11.11)
この記事へのコメントは終了しました。
コメント