pusher / chatkit-client-js

JavaScript client SDK for Pusher Chatkit
https://pusher.com/chatkit
MIT License
90 stars 15 forks source link

Fails to run in node environment (v9.11.1) #70

Closed hugomd closed 4 years ago

hugomd commented 6 years ago

Hey 👋

First off, thanks for all the awesome work you do, Pusher is awesome 🚀

I've run into issues using this library on node (using v9.11.1) — just requiring it fails with errors like:

ReferenceError: window is not defined ``` /Users/hugo/git/pusher-relay-chat/client/node_modules/@pusher/chatkit/dist/web/chatkit.js:1 (function (exports, require, module, __filename, __dirname) { !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.Chatkit=t()}(this,function(){"use strict";"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self&&self;var e,t,n=(function(e,t){var n;n=function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:r})},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=10)}([function(e,t,n){function r(e){var t={};if(!e)return t;for(var n=0,r=e.s ReferenceError: window is not defined at new e (/Users/hugo/git/pusher-relay-chat/client/node_modules/@pusher/chatkit/dist/web/chatkit.js:1:2031) at new e (/Users/hugo/git/pusher-relay-chat/client/node_modules/@pusher/chatkit/dist/web/chatkit.js:1:6764) at new e (/Users/hugo/git/pusher-relay-chat/client/node_modules/@pusher/chatkit/dist/web/chatkit.js:1:78367) at Object. (/Users/hugo/git/pusher-relay-chat/client/index.js:25:21) at Module._compile (internal/modules/cjs/loader.js:654:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10) at Module.load (internal/modules/cjs/loader.js:566:32) at tryModuleLoad (internal/modules/cjs/loader.js:506:12) at Function.Module._load (internal/modules/cjs/loader.js:498:3) at Function.Module.runMain (internal/modules/cjs/loader.js:695:10) ```

Found this a little odd, given that the roll up config specifies the build for umd, but I'm not familiar with how roll up works 🤷‍♂️ https://github.com/pusher/chatkit-client-js/blob/3c6ee1fe05997782c803e8890ca8e405def77b60/rollup/web.js#L8

I've managed to get around this using JSDOM to define a fake window and navigator global:

const {JSDOM} = require('jsdom');
const {ChatManager, TokenProvider} = require('@pusher/chatkit');

// Hack so that @pusher/chatkit can be used
const {window} = new JSDOM();
global.window = window;
global.navigator = {};

const chatManager = new ChatManager({
  instanceLocator,
  userId,
  tokenProvider: new TokenProvider({url: 'http://localhost:3000/auth'}),
});

Let me know if there's any other information I can provide 🙏

callum-oakley commented 6 years ago

Hi :wave:

Node isn't officially supported yet because we use XHR etc under the hood. We would like to officially support node at some point though so watch this space!

It's good to see that things seem to work with JSDOM! Thanks for the information on the workaround :)

callum-oakley commented 4 years ago

Closing this as stale. Node support is in our internal backlog but it's not something that we're likely to pick up in the imminent future. Please contact support if this issue is important for your use case!