question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

dynamic import of `canvas` causes bundling problems

See original GitHub issue

Unfortunately, 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:

https://github.com/jsdom/jsdom/blob/16d3913eea0360c7757e75f7e266c3873c85b7dd/lib/jsdom/utils.js#L163-L177

The problem it’s causing has an issue here:

https://github.com/zeit/ncc/issues/274

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
domeniccommented, Feb 12, 2019

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.

1reaction
akirkcommented, Feb 11, 2021

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 remap canvas to the util package like this in webpack.config.js:

externals: {
	canvas: "util"
}

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") to require("util") (actually something like __webpack_require__(/*! canvas */ \"util\");) which succeeds which in turn prevents the error message. Since the util function is part of node, it doesn’t add to the weight of the bundle.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found