Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hugomd opened this issue Apr 26, 2018 · 1 comment
Open

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

hugomd opened this issue Apr 26, 2018 · 1 comment

Comments

@hugomd
Copy link

@hugomd 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

This comment has been minimized.

Copy link
Member

@callum-oakley callum-oakley commented Apr 26, 2018

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.