js-graph-algorithms icon indicating copy to clipboard operation
js-graph-algorithms copied to clipboard

When using webpack 5, cannot import this package.

Open leokolezhuk opened this issue 3 years ago • 2 comments

Upgraded to webpack 5.7 from 4.x and VueCli 5 from 4.

In an es module using the js-graph-algorithms package. const jsgraphs = require('js-graph-algorithms'); In this case jsgraphs is an empty object and errors are raised like Cannot read property Graph of undefined when trying to access jsgraphs.Graph.

This is caused by the way jsgraphs object is exported in src/jsgraphs.js The code var module = module || {}; if(module) { module.exports = jsgraphs; } seems to confuse webpack.

If I change this code to a normal commonjs export: module.exports = jsgraphs; without any extra logic, everything works normally.

leokolezhuk avatar Oct 07 '22 13:10 leokolezhuk

As a workaround, patching this package upon installation with patch-package.

The patch I use is the following:

  diff --git a/node_modules/js-graph-algorithms/src/jsgraphs.js b/node_modules/js-graph-algorithms/src/jsgraphs.js
  index 5409240..3c8f82b 100644
  --- a/node_modules/js-graph-algorithms/src/jsgraphs.js
  +++ b/node_modules/js-graph-algorithms/src/jsgraphs.js
  @@ -1202,7 +1202,4 @@ var jsgraphs = jsgraphs || {};
       jss.FordFulkerson = FordFulkerson;
   })(jsgraphs);
   
  -var module = module || {};
  -if(module) {
  -	module.exports = jsgraphs;
  -}
  \ No newline at end of file
  +module.exports = jsgraphs;

leokolezhuk avatar Oct 21 '22 15:10 leokolezhuk

Also having this problem

NilsBaumgartner1994 avatar Oct 27 '22 18:10 NilsBaumgartner1994