Fabric.js: Make compatible with latest node.js

Created on 22 Dec 2016  路  11Comments  路  Source: fabricjs/fabric.js

As discussed in #3366 version 1.7.x doesn't work with the latest version of node because it depends on contextify which doesn't compile anymore. I believe there is a simple workaround for this:

  • install canvas, jsdom and xmldom: npm install canvas jsdom xmldom
  • install fabric.js without optional dependencies: npm install --no-optional fabric
  • patch dist/fabric.js by replacing
if (fabric.document.createWindow) {
  fabric.window = fabric.document.createWindow();
} else {
  fabric.window = fabric.document.parentWindow;
}

with:

if (fabric.document.createWindow) {
  fabric.window = fabric.document.createWindow();
} else if(fabric.document.parentWindow) {
  fabric.window = fabric.document.parentWindow;
} else {
  fabric.window = fabric.document.defaultView;
}

Could this patch be added?

Most helpful comment

you have to install jsdom and node-canvas to make it work.

All 11 comments

fabric support and can be used with node up to 6.
which version are you using?

Sorry, I'm using 7.2.1. I just saw that you already use document.defaultView in the fabric2 branch. Until that is stable I'll consider downgrading Node.

I think this issue needs to be open.
@Lebuin thanks for the hack, it helped me on node 7.3.0

untill we switch to 2.0 node 7 is unsupported. that's it.

I had the same problem as discussed in #3342 when using node 7.0.0 and above, so I tried running 6.9.1 but then encountered problems with contextify like #3366. I tried running npm install with --no-optional as suggested, which worked to install but then crashed my app (contextify complained that I needed NODE_MODULE_VERSION 51 and was running NODE_MODULE_VERSION 48).

Anyway, that forced me to use node 7.0.0 or up, and this workaround worked great! Thank you!

Had dependency issue until using node 6.x.x from 7.x.x -- works! Thank you

on os x after install node 6.9.1, it seems ok

Could someone confirm if node 8.9.1 is working for them? Trying to figure out if I need to back track to a previous version of node LTS (would prefer to stay at 8.x if at all possible). I'm having similar issues .. WITH TESTS ONLY .. wherein I instantiate a new canvas: new fabric.Canvas(), but receive TypeError: Cannot read property 'imageSmoothingEnabled' of null. A fuller snippet of code using (node 8.9.1, all of these (pkg-config cairo libpng jpeg giflib) are installed, and finally, I'm on fabricjs 2.0.0-rc3:

import { fabric } from 'fabric';
import React from 'react';

describe('do a thing', () => {
  it('should render things', () => {
    const canvas = new fabric.Canvas();  // ERRORs here, with: `TypeError: Cannot read property 'imageSmoothingEnabled' of null`
    const component = shallow(<MyCanvas canvas={canvas} />);
    expect(component).toBeDefined();
  })
})

Any suggestions/thoughts/etc?

Thanks much!

you have to install jsdom and node-canvas to make it work.

@asturur I'm so sorry. :( :hangs-head-in-shame:

can definitely close my issue as part of this overall issue. :)

@megalithic can you shared how you solve it ?
I can make it work using jsdom and node canvas.
I'm using node v8.12.0 and fabric 2.5.0

Was this page helpful?
0 / 5 - 0 ratings