1

I am migrating my reactjs app in ES6. It used some external javascript which defines global variables in old way and no exports. There is no way i can change and maintain the same. When a function inside that file is referenced, I get uncaught referenceerror for the functions defined without keyword var/const.

What is the correct way to use this kind of files in ES6?

e.g.If an old third party js file contains following code. I get an uncaught reference error for ClassicClient during execution in browser.

ClassicClient = function(x, y){
  return x+y;
}
sdgluck
  • 24,894
  • 8
  • 75
  • 90
Amit Teli
  • 875
  • 11
  • 25
  • Can I please know the reason for the down vote please? It is a genuine problem with libraries external to my app. – Amit Teli Dec 08 '16 at 14:29
  • Just do not transpile this file.. but I do not understand how any third party library could define functions or/and variables without `var` – nicovank Dec 08 '16 at 14:56
  • 1
    There is really nothing different between ES5 and ES6 here. As long as your global functions are defined before the transpiled ES6 code is executed, you should be fine. Do you embed the external JS files in your page before your own? – TimoStaudinger Dec 08 '16 at 15:16
  • I do think that they are defined before the execution. I am not including it in an html page directly. It is getting referred inside one reactjs container. I thought it has to do with ES6 because that is the big change in my stack. I also agree with @nicovank that this library itself should not have used declared anything with var/const but currently I am stuck with it. – Amit Teli Dec 08 '16 at 16:39

1 Answers1

0

Since you're probably using something (Babel?) to transpile from ES6 to ES5 and also some module bundler (Webpack?), your bundle has "use strict", making it run on Strict Mode, which doesn't allow global vars without window. prefix. See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode#Converting_mistakes_into_errors

I'm not sure if it's possible to disable strict mode for one specific module or just globally. Check: How to remove global "use strict" added by babel

Community
  • 1
  • 1
fjsj
  • 10,995
  • 11
  • 41
  • 57
  • Yes, it looks to be the situation. These files do not export anything and I cannot refer the variables/functions inside. I have discontinued usage of the same for now. Appreciate your time for the answering though. – Amit Teli Jan 09 '17 at 12:38