Jsdom: "request of a dependency is an expression" (When bundling with Webpack)

Created on 24 Nov 2017  ·  5Comments  ·  Source: jsdom/jsdom

Basic info:

  • Node.js version: 8.9.1
  • jsdom version: 11.4.0

When bundling my server application (which internally uses JSDOM) with web pack, I get the following warning:

WARNING in .../node_modules/jsdom/lib/jsdom/utils.js
    203:21-40 Critical dependency: the request of a dependency is an expression

The code in question is:

["canvas", "canvas-prebuilt"].some(moduleName => {
  try {
    exports.Canvas = require(moduleName);

While it might not be extremely common to bundle the server code, I find it quite nice to go from 266MB (src/ and node_modules/) to a single 6MB JS file (in my case).

Would you consider rewriting the above import to be always static? Or do you see this as a deficit in the bundler that it can't detect that in practice the possible modules could be inferred?

(If it were rewritten to an if with literal require parameters, users might still need to add an exception as neither canvas nor canvas-prebuilt are available by default, but then at least the problem would explicitly have been handled (and the user would have to be sure that they're not using these)).

Most helpful comment

update! :raised_hands:

the fix is super simple, and I got it from a mocha-webpack issue: https://github.com/zinserjan/mocha-webpack/issues/6#issuecomment-213957809. Also there is this document https://github.com/zinserjan/mocha-webpack/blob/master/docs/installation/webpack-configuration.md that recommends using this plugin.

const nodeExternals = require('webpack-node-externals');

module.exports = {
  externals: [nodeExternals()],
  // etc configs here
}

All 5 comments

No, sorry, webpack is not supported.

@domenic Why is webpack not supported? It's by far the most popular bundler.

Because it can't handle code patterns that jsdom uses.

@domenic what kind of code patterns? webpack has a flurry of releases all the time these days, and there are a myriad of plugins. I'm sure we can come up with some good practices for using with webpack. :D

I already use jsdom with webpack, and minimally the document/dom methods I'm using are working fine, I just came to this issue because of the warning above. I only noticed it now because I think mocha-webpack was suppressing the warning, and I noticed after tossing it. however, it could be that mocha-webpack isnt suppressing anything, and is doing something smart to handle this and we could use that best practice.

update! :raised_hands:

the fix is super simple, and I got it from a mocha-webpack issue: https://github.com/zinserjan/mocha-webpack/issues/6#issuecomment-213957809. Also there is this document https://github.com/zinserjan/mocha-webpack/blob/master/docs/installation/webpack-configuration.md that recommends using this plugin.

const nodeExternals = require('webpack-node-externals');

module.exports = {
  externals: [nodeExternals()],
  // etc configs here
}
Was this page helpful?
0 / 5 - 0 ratings