cytoscape/cytoscape.js-elk

"self is not defined" exception in nodejs

kobajder opened this issue · 6 comments

Affected versions: 2.0.0, 2.0.1, 1.2.2 is not affected

Problem
When executed by nodejs, it throws uncaught exception: self is not defined at cytoscape-elk/dist/cytoscape-elk.js:10:4 (as reported by the 2.0.1), but the same uncaught exception is raised also by the 2.0.0

Steps to reproduce

  1. create test.js file with the following content:
    const cytoscape = require('cytoscape');
    const elk = require('cytoscape-elk');
    cytoscape.use(elk);
  2. execute the file by nodejs: node ./test.js

Reported on environment
OS: Windows 10 Pro, Linux Debian 4.9
Nodejs: 12.18.1, 12.16.1, 14.15.5

Please provide a reproducible test case, including:

  • Browser version
  • OS version
  • A code sample that reproduces the issue. A JSBin can be forked to easily provide a code sample.

It's important to provide a reproducible demo. Without that, it's not practical to verify that there is indeed a bug in the library -- or whether there is a bug in the library consumer's app. It's easy and quick to create a demo using JSBin if you have a reproducible issue.

More often than not, issue reports without a demo are due to bugs in the consumer's code. So unfortunately, it's not a good use of our limited resources to spend time investigating which which issue reports are genuine.

As soon as you provide a reproducible demo, this can be investigated further. Thanks for your feedback

The requested information has been provided. It's part of the description now

@akx, would this be related to your upgrades of ELK in #15?

akx commented

@maxkfranz Not that in particular, it has to do with the Webpack UMD configuration changing.

To make UMD build available on both browsers and Node.js, set output.globalObject option to 'this'. Defaults to self for Web-like targets.
https://webpack.js.org/configuration/output/#outputglobalobject

However, changing that to this uncovers another issue:

node_modules/cytoscape/dist/cytoscape.cjs.js:31647
  ext.apply(null, args);
      ^

TypeError: ext.apply is not a function
    at Function.cytoscape.use (node_modules/cytoscape/dist/cytoscape.cjs.js:31647:7)

This is caused by an "impedance mismatch" between CJS and ES modules' default exports; the workaround is to do

const cytoscape = require('cytoscape');
const elk = require('cytoscape-elk');
cytoscape.use(elk.default);

but I'll see if I can tell Webpack to bundle things in a way that doesn't require the additional default for CJS environments.

akx commented

@maxkfranz PR is in: #26

@akx's PR has been released as v2.0.2. Thanks for the PR!