twilio / twilio-chat-demo-js

Programmable Chat API Demo Application for JavaScript
BSD 3-Clause "New" or "Revised" License
90 stars 94 forks source link

global is not defined #41

Closed adamduren closed 2 years ago

adamduren commented 5 years ago

Attempting to use twilio-chat in an Angular project with TypeScript. I've tried various methods of importing and they all result in this error when trying to instantiate the Client.


import * as Chat from 'twilio-chat';
const client = await Chat.create(token); // ERR

import Client from 'twilio-chat';
const client = await Client.create(token); // ERR

import { Client } from 'twilio-chat';
const client = await Client.create(token); // ERR
core.js:15714 ERROR Error: Uncaught (in promise): ReferenceError: global is not defined
ReferenceError: global is not defined
    at Object../node_modules/twilsock/lib/tokenStorage.js (tokenStorage.js:51)
    at __webpack_require__ (bootstrap:83)
    at Object../node_modules/twilsock/lib/client.js (client.js:14)
    at __webpack_require__ (bootstrap:83)
    at Object../node_modules/twilsock/lib/index.js (index.js:3)
    at __webpack_require__ (bootstrap:83)
    at Object../node_modules/twilio-chat/browser/client.js (client.js:71)
    at __webpack_require__ (bootstrap:83)
    at Object../node_modules/twilio-chat/browser/index.js (index.js:3)
    at __webpack_require__ (bootstrap:83)
    at resolvePromise (zone.js:814)
    at resolvePromise (zone.js:771)
    at zone.js:873
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:421)
    at Object.onInvokeTask (core.js:17280)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:420)
    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:188)
    at drainMicroTaskQueue (zone.js:595)

Inspecting tokenStorage.js reveals the culprit two lines.

/**51 **/ TokenStorage.sessionStorage = global['sessionStorage'];
/**52 **/ TokenStorage.window = global['window'];

Adding (window as any).global = window; is a workaround but one that should not be needed.

I'm not sure if this is the correct place to file issues as the twilio-chat library does not seem to be open source and there isn't a bugs entry in package.json.

joelcogen commented 5 years ago

I'm having the same issue but in my case the (window as any).global = window; trick doesn't work either. Did you find a better fix?

amatelic commented 4 years ago

i am encountering the same problem as above. And the hack (window as any).global = window; is not.

dryize commented 4 years ago

I've added, window.global = window; in head of my index.html and it worked.

miktav commented 4 years ago

I've added, window.global = window; in head of my index.html and it worked.

This worked for me, too! Thanks so much.

gkucmierz commented 4 years ago

I've added, window.global = window; in head of my index.html and it worked.

Works, but it is very dirty solution. @dmorilha-twilio