Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

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

Closed
hugomd opened this issue Apr 26, 2018 · 2 comments
Closed

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

hugomd opened this issue Apr 26, 2018 · 2 comments

Comments

@hugomd
Copy link

hugomd commented Apr 26, 2018

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.<anonymous> (/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 πŸ€·β€β™‚οΈ

format: 'umd',

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
Copy link
Contributor

Hi πŸ‘‹

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
Copy link
Contributor

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!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants