dynamic import of `canvas` causes bundling problems
See original GitHub issueUnfortunately, the dynamic import of canvas
trips up some bundlers that are trying to include all requirements. Perhaps the dynamic import can be further obfuscated, so it’s not discoverable via static analysis?
All relevant code is here:
The problem it’s causing has an issue here:
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Code splitting in webpack with dynamic imports - Medium
With the advent of package bundlers like webpack, the general tendency is to combine all the JS code into a single bundle.js file....
Read more >Angular: Dynamic Importing Large Libraries | lacolaco/tech
This post explains how to import large 3rd-party libraries into your Angular application without pain on initial payload size.
Read more >Web Workers - Stencil.js
The problem is that long-running JS prevents the browser from running smooth animations (CSS animations, transitions, canvas, svg...), making your site look ...
Read more >Dynamic Imports - Learn JavaScript | W3Docs Tutorial
The simplicity of the export/import structure makes all that possible. Now, we are going to explore the ways of importing a module dynamically,...
Read more >JavaScript modules - MDN Web Docs
Use of native JavaScript modules is dependent on the import and export statements ... Report problems with this compatibility data on GitHub ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Sorry, but we target Node.js, not bundlers. If bundlers are unable to handle this but still claim to support Node.js code, then they have a bug, or need more configuration.
Closing as working-as-intended, but feel free to discuss in the closed thread with others who might have an idea on how to configure your bundler.
If you don’t want to use canvas and need avoid the error message
Module not found: Error: Can't resolve 'canvas'
when bundling with Webpack (for example because of a CI), it worked for me to remapcanvas
to theutil
package like this inwebpack.config.js
:Since jsdom checks the Canvas object for
typeof Canvas.createCanvas === "function"
it will fail and thus not enable canvas support.Webpack rewrites the
require("canvas")
torequire("util")
(actually something like__webpack_require__(/*! canvas */ \"util\");
) which succeeds which in turn prevents the error message. Since theutil
function is part of node, it doesn’t add to the weight of the bundle.