fix usage of global object in browser environment / Angular no longer supporting it #1088

Closed
opened 2018-05-02 18:16:01 +00:00 by cristhiank · 5 comments
cristhiank commented 2018-05-02 18:16:01 +00:00 (Migrated from github.com)

Browser code should not rely on things that are not available in browser environments. Otherwise it throws error global is undefined.

see angular-cli issue

Browser code should not rely on things that are not available in browser environments. Otherwise it throws error global is undefined. see [angular-cli issue](https://github.com/angular/angular-cli/issues/9827#issuecomment-369578814)
SheetJSDev commented 2018-05-02 20:12:11 +00:00 (Migrated from github.com)

This hasn't been an issue in angular 2/4/5. Can you share a sample project using a beta version of angular 6?

This hasn't been an issue in angular 2/4/5. Can you share a sample project using a beta version of angular 6?
cristhiank commented 2018-05-03 00:35:49 +00:00 (Migrated from github.com)

Sure, you can check in this link
Just clone and run ng serve and you will see the following error in the console:
image

Sure, you can check in [this link](https://github.com/cristhiank/xlsx-issue) Just clone and run ng serve and you will see the following error in the console: ![image](https://user-images.githubusercontent.com/1218220/39555597-ff568ca0-4e3f-11e8-883b-e3bb28ffaab6.png)
cristhiank commented 2018-05-03 20:21:57 +00:00 (Migrated from github.com)

Just adding some information: Angular v6 was released today.

Just adding some information: Angular v6 was released today.
SheetJSDev commented 2018-05-03 20:32:01 +00:00 (Migrated from github.com)

We're going to cut a new release today. Based on some tests against your sample project, it should be sufficient to add a global typeof guard:

 if(typeof module !== "undefined" && typeof require !== 'undefined') {
-       if(typeof cptable === 'undefined') global.cptable = require('./dist/cpexcel.js');
+       if(typeof cptable === 'undefined') {
+               if(typeof global !== 'undefined') global.cptable = require('./dist/cpexcel.js');
+               else if(typeof window !== 'undefined') window.cptable = require('./dist/cpexcel.js');
+       }
 }

We're going to cut a new release today. Based on some tests against your sample project, it should be sufficient to add a global typeof guard: ```diff if(typeof module !== "undefined" && typeof require !== 'undefined') { - if(typeof cptable === 'undefined') global.cptable = require('./dist/cpexcel.js'); + if(typeof cptable === 'undefined') { + if(typeof global !== 'undefined') global.cptable = require('./dist/cpexcel.js'); + else if(typeof window !== 'undefined') window.cptable = require('./dist/cpexcel.js'); + } } ```
cristhiank commented 2018-05-03 21:03:46 +00:00 (Migrated from github.com)

Yes, that will work! Thanks for the quick reply.

Yes, that will work! Thanks for the quick reply.
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sheetjs/sheetjs#1088
No description provided.