Is it possible to use jsdom without canvas support?
See original GitHub issueI don’t need canvas support and I’ve been getting this error when importing jsdom into my project:
19 01 2017 13:07:19.722:ERROR [karma]: Error: Cannot find module ‘canvas’ from ‘.’
I’m not using the <canvas>
element on my code so I don’t understand why I get that error. This should only happen if the canvas
package is actually used for anything. But since I don’t have <canvas>
elements, why is it being used?
I could install the canvas
package but I’m on Windows and that has a lot of dependencies just to install a simple package. I really didn’t want to have all that work for a package I don’t need.
Can I workaround this without having to install the canvas
package?
EDIT:
Looking at the README:
jsdom includes support for using the canvas package to extend any
<canvas>
elements with the canvas API. To make this work, you need to include canvas as a dependency in your project, as a peer of jsdom. If jsdom can find the canvas package, it will use it, but if it’s not present, then<canvas>
elements will behave like<div>
s.
Why is my test suite crashing when I import jsdom
into my spec files without having the canvas
package installed?
Issue Analytics
- State:
- Created 7 years ago
- Comments:21 (4 by maintainers)
Top GitHub Comments
I was able to work around this by adding
"canvas": "file:./canvas"
to the dependencies section of the app’s package.json and creatingcanvas/index.js
containing simplymodule.exports = {}
.That was enough to persuade jsdom that canvas wasn’t available, after building the app with
@zeit/ncc
(webpack).Landing here from a google search for this specific scenario - i’m also using webpack with lambda.
The trick is to tell webpack to provide an empty stub of canvas:
Note the quotes around the braces. This creates an empty object. If you use
{}
, you wind up getting an error “Cannot read property ‘createCanvas’ of undefined”.