Native ES6 modules vs. the bundle
When plotly.js is loaded in Chrome by the native ES6 import, the loading crashes with Cannot read property 'document' of undefined at line 18178, i.e. at var d3_document = this.document;
The problem is that the bundler assumes that when a function is executed without any this, the this will automatically be set to window. This is not the case with ES6 modules, where this will be undefined.
I fixed it by changing line 27724 from }(); to }.apply(self); but this is just a hack :)
Thanks for writing in.
When
plotly.jsis loaded in Chrome by the native ES6import,
Can you share a reproducible example of how you're going about doing this. Thank you!
Try:
<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
</head>
<body>
<script type="module" src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</body>
</html>
or alternatively:
<script type="module">
import 'https://cdn.plot.ly/plotly-latest.min.js'
</script>
In a codepen: https://codepen.io/alexcjohnson/pen/aXEbOa?editors=1011
That particular error is with d3v3 - seems like v4 fixes it so we should get it when we leapfrog to v5. Will be interesting to see if there's anything else in our code that uses this to mean the window (or anything else that's incompatible with ES6 modules... I'm not very familiar) but it doesn't seem like there's much point trying before the d3 upgrade.
@oldrich-svec
I fixed it by changing line
27724from}();to}.apply(self);but this is just a hack :)
Can you please provide which particular line had to be changed. I am stuck with this for the past two days (tried many things with babel options - nothing worked)
Any fix for this?
Fix for version 1.48.2; the change is at line number 32481 '/node_modules/plotly.js/dist/plotly.js'
Anybody know if there is a issue for this filed in the d3 project?
@jmsuresh In this file:
https://cdnjs.cloudflare.com/ajax/libs/plotly.js/1.49.1/plotly.js
search for define(d3). You will find:
if (typeof define === "function" && define.amd) this.d3 = d3, define(d3); else if (typeof module === "object" && module.exports) module.exports = d3; else this.d3 = d3;
}();
Change }(); to }.apply(self);
From https://github.com/plotly/plotly.js/issues/4130 @Arjun-Shinojiya wrote:
I am trying to integrate plotly.js in my Angular 8 project. I successfully integrated it and tested it in local ,that time it is working fine without any error. But when I check it in live server ,that time I am getting cannot read property 'document ' of undefined error.. I also similar issue blog in this repository.`.I wanted to try this hack changing line 27724 from }(); to }.apply(self); but this is just a hack :) But I dont know in which module file I can get this line.Currently none of the plotly.js file have 27724 lines
Maybe someone here can help.
When
plotly.jsis loaded in Chrome by the native ES6import, the loading crashes withCannot read property 'document' of undefinedat line18178, i.e. atvar d3_document = this.document;The problem is that the bundler assumes that when a function is executed without any
this, thethiswill automatically be set towindow. This is not the case with ES6 modules, wherethiswill be undefined.I fixed it by changing line
27724from}();to}.apply(self);but this is just a hack :)
@oldrich-svec can you please tell the exact location of the file which you mentioned in this questions last line? .I am facing the same issue.
see https://github.com/plotly/plotly.js/issues/3518#issuecomment-517138019
@Arjun-Shinojiya it is up to you how you use plotly.js ;). The comment just shows you what line you want to edit.
Is editing the line of code the only solution? What happens when I want to upgrade to a new version of plotly? I have to edit it every time?
@TravisLRiffle yes you have to reapply your hack every time! For me, it is not a solution for enterprise applications.
After fixing this, I got a d3_xhr-not found-error although it's defined
Alternative workaround for Angular 8 upgraders: For those of us coming here having encountered this issue when upgrading to Angular 8, an alternative workaround solution to editing the plotly.js file is to not use the new "Differential Loading by Default" as mentioned in this comment on angular-plotly.js: https://github.com/plotly/angular-plotly.js/issues/75#issuecomment-526871836
I could solve the issue forcing the typescript to be generated as "target": "es5" making it not use the , thus both plotly.js and angular code is served by "legacy" way. Please check this link for more informations: https://blog.angular.io/version-8-of-angular-smaller-bundles-cli-apis-and-alignment-with-the-ecosystem-af0261112a27
So changing the following property in the tsconfig.json file hides the issue:
{
"compilerOptions": {
"target": "es5"
}
}
Is there a 'real' solutions for this available? I'd like to use Plotly for an Angular 8 project - but reverting to es5 is not really an option (it will break for Angular 9 I think). I was so hoping to use plotly as a Highchart replacement - but no dice I'm afraid.
Just tried with the current latest version (1.52.2), and it's still broken. Angular 9 has also been released, so it's now the two latest versions that are affected. :(
We have a large Angular-based application, that we cannot update because of this issue. :(
Just tried with the current latest version (1.52.2), and it's still broken. Angular 9 has also been released, so it's now the two latest versions that are affected. :(
We have a large Angular-based application, that we cannot update because of this issue. :(
First, I would recommend taking a look at https://github.com/plotly/angular-plotly.js as that is apparently working with Angular 8.x+.
Otherwise, if it is an option for your project, I would recommend loading plotly.js via the CDN. I have upgraded to Angular 9 by lazy-loading the minified plotly.js this way and it is working very well. The main bundle is then also much smaller :-) (I'm no longer using the workaround I mentioned in my previous comment)