sockjs-client: global is not defined

What is global and why it is not defined?

Uncaught ReferenceError: global is not defined
    at Object../node_modules/sockjs-client/lib/utils/browser-crypto.js (browser-crypto.js:3)
    at __webpack_require__ (bootstrap:76)
    at Object../node_modules/sockjs-client/lib/utils/random.js (random.js:4)
    at __webpack_require__ (bootstrap:76)
    at Object../node_modules/sockjs-client/lib/utils/event.js (event.js:3)
    at __webpack_require__ (bootstrap:76)
    at Object../node_modules/sockjs-client/lib/transport/websocket.js (websocket.js:3)
    at __webpack_require__ (bootstrap:76)
    at Object../node_modules/sockjs-client/lib/transport-list.js (transport-list.js:5)
    at __webpack_require__ (bootstrap:76)

node -v v10.0.0 npm -v 6.1.0 sockjs-client 1.1.4

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 13
  • Comments: 16 (1 by maintainers)

Most upvoted comments

@cawena just define it yourself before other js script files, eg. in head section:

<script>
    var global = window;
</script>

use built version import sockjs from "sockjs-client/dist/sockjs"

Thanks @rechardlc that worked for me!

For TS folks, I got the types back by adding a sockjs-client.d.ts with

declare module "sockjs-client/dist/sockjs" {
  export default (await import("sockjs-client")).default
}

is there a proper fix planned to be added ?

global is only available in a Node.js environment. If you want to use this library in a browser, you need to use the built version or build it yourself. In my build process, browserify is used to replace global with window among other things.

any updates?

I have the same issue when I upgrade Angular from 5.X to 6.x and rxjs 5.x to 6.x. What I have to change?

This one works fine for me.

sockjs-client.d.ts

declare module 'sockjs-client/dist/sockjs' {
  import SockJS from '@types/sockjs-client';

  export = SockJS;
  export as namespace SockJS;
}

somecode.ts

import SockJS from 'sockjs-client/dist/sockjs';

This one works fine for me.

sockjs-client.d.ts

declare module 'sockjs-client/dist/sockjs' {
  import SockJS from '@types/sockjs-client';

  export = SockJS;
  export as namespace SockJS;
}

somecode.ts

import SockJS from 'sockjs-client/dist/sockjs';

it works, thanks 🎉

You might get eslint / build errors in this code block:

declare module "sockjs-client/dist/sockjs" {
  export default (await import("sockjs-client")).default
}

If so, then use this one instead (copy-pasted from @types/sockjs-client and re-arranged a little):

declare namespace SockJSN {
  type CONNECTING = 0;
  type OPEN = 1;
  type CLOSING = 2;
  type CLOSED = 3;

  type State = CONNECTING | OPEN | CLOSING | CLOSED;

  interface BaseEvent extends Event {
    type: string;
  }

  type OpenEvent = BaseEvent;

  interface CloseEvent extends BaseEvent {
    code: number;
    reason: string;
    wasClean: boolean;
  }

  interface MessageEvent extends BaseEvent {
    data: string;
  }

  type SessionGenerator = () => string;

  interface Options {
    server?: string | undefined;
    sessionId?: number | SessionGenerator | undefined;
    transports?: string | string[] | undefined;
    timeout?: number | undefined;
  }
}

declare module 'sockjs-client/dist/sockjs.js' {
  const SockJS: {
    new (
      url: string,
      _reserved?: unknown,
      options?: SockJSN.Options
    ): WebSocket;
    (url: string, _reserved?: unknown, options?: SockJSN.Options): WebSocket;
    prototype: WebSocket;
    CONNECTING: SockJSN.CONNECTING;
    OPEN: SockJSN.OPEN;
    CLOSING: SockJSN.CLOSING;
    CLOSED: SockJSN.CLOSED;
  };

  export default SockJS;
}

To use SockJS in your .ts files, import it like this:

import SockJS from 'sockjs-client/dist/sockjs.js';