Skip to content
This repository has been archived by the owner on Nov 5, 2022. It is now read-only.

After update to 0.1.2 version getting error #11

Open
mehtaad opened this issue May 2, 2017 · 16 comments
Open

After update to 0.1.2 version getting error #11

mehtaad opened this issue May 2, 2017 · 16 comments

Comments

@mehtaad
Copy link

mehtaad commented May 2, 2017

I am getting error after updating to newer version,
ERROR in .//canvas-5-polyfill/canvas.js
Module not found: Error: Can't resolve 'canvas' in 'D:\src\node_modules\canvas-5-polyfill'
@ ./
/canvas-5-polyfill/canvas.js 14:29-46

@kepeterson
Copy link

Same here, here's the offending line in canvas.js :

if (CanvasRenderingContext2D == undefined) { CanvasRenderingContext2D = require('canvas').Context2d; }

@jcgregorio
Copy link
Contributor

OK, I will push a 0.1.3 that fixes this soon.

@jcgregorio
Copy link
Contributor

I just published 0.1.3, let me know if that fixes the issue.

@jcgregorio
Copy link
Contributor

BTW, what platform was this occurring on?

@talltyler
Copy link

You can't have conditional requires in tools like babel because everything is compiled into the bundle that is pushed to browsers. I think your best best is to make two end point versions of this module, one for node canvas and one for everything else. Alternatively you could have a static function that you pass in the prototype to extend.

I use Node Canvas in my work and personally I vote on not supporting it as a first class citizen. It's api doesn't follow the standards. To get around this and the other issues with older canvas implementations in my projects I write adapters in front of the CanvasRenderingContext2D to get around the inconsistencies between Node Canvas and the browsers. If you implement the method where a prototype can be passed in I can just pass in my adapter, but for everyone else having two api end points is likely going to be the simplest.

@kepeterson
Copy link

I'm running macOS Sierra 10.12.4 and I was using canvas-5-polyfill with Node Canvas.

@mehtaad
Copy link
Author

mehtaad commented May 3, 2017

I am using windows 7 Professional, node, ES6 and bable

@jcgregorio
Copy link
Contributor

@talltyler "I think your best best is to make two end point versions of this module, one for node canvas and one for everything else."

That does sound like a good fix, could you submit that as a pull request?

@talltyler
Copy link

I've looked into doing this and I'm not sure I'm the person to do this. It looks to me as though this repo should be broken up into a few parts. First supporting the feature and then a different module for making that work within Node Canvas. I would also be tempted to rename this project Path2D-polyfill or something like that.

@cartant
Copy link
Contributor

cartant commented May 17, 2017

Simply adding a browser field to the package.json would go a long to solving this for most people:

"browser": {
  "canvas": false
}

Doing so will see Browserify and Webpack ignore the require('canvas'); calls.

For more information, see here.


Actually, that's not the only problem. Declaring CanvasRenderingContext2D as a var:

var CanvasRenderingContext2D = CanvasRenderingContext2D;

Ensures that it will always be undefined, as the variable's name shadows the global name.

@cartant
Copy link
Contributor

cartant commented Jun 4, 2017

#14 should have addressed this issue. Is a release that includes #14 imminent?

@jcgregorio
Copy link
Contributor

Sorry for the delay, I just published the 0.1.5 release which includes this fix.

@diegodoumecq
Copy link

Using webpack and trying to import this file gives me the same "Can't resolve 'canvas'" error. (I'm using v0.1.5)

@cartant
Copy link
Contributor

cartant commented Aug 19, 2017

@diegodoumecq It seems that Webpack performs some static analysis and is able to determine that require has been assigned to nodeRequire and then attempts to bundle canvas - which should be used only under Node. If you manage your own Webpack configuration, you might be able to use the externals mechanism outlined here.

If something else - e.g. @angular/cli or create-react-app - manages it, there's not much you can do. This package would need to be modified.

@ForesightImaging
Copy link

I'm new to webpack, but found myself here.... I was able to get webpack happy for my own dependency on canvas-5-polyfill using the addition of:
module.exports = {
externals:{'canvas':'canvas'}, <=========

To prevent the require from failing.

@borm
Copy link

borm commented Dec 13, 2018

Up, the same problem

ERROR in ../node_modules/canvas-5-polyfill/canvas.js
Module not found: Error: Can't resolve 'canvas' in '/web/node_modules/canvas-5-polyfill'
 @ ../node_modules/canvas-5-polyfill/canvas.js 16:29-50

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants