sockjs / sockjs-client

WebSocket emulation - Javascript client
MIT License
8.45k stars 1.3k forks source link

global is not defined #439

Closed artuska closed 6 years ago

artuska commented 6 years ago

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

brycekahle commented 6 years ago

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.

koulombus commented 6 years ago

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?

artuska commented 6 years ago

@cawena yeap, exactly the same situation. Maybe Angular or rxjs have had global variable defined in global scope but in the latest releases they dropped it from the global scope so it became undefined :(

koulombus commented 6 years ago

@artuska do you have some solution of this problem?

artuska commented 6 years ago

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

<script>
    var global = window;
</script>
evgenyfedorenko commented 6 years ago

is there a proper fix planned to be added ?

ihor-zinchenko commented 4 years ago

any updates?

rechardlc commented 2 years ago

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

mwalkerr commented 2 years ago

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
}
izogfif commented 1 year ago

Why not just use globalThis?

izogfif commented 1 year ago

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';
scottfwalter commented 8 months ago

@izogfif That ds.t works great for my Typescript library project. However, anytime I attempt to use it in a unit test with Karma and karma-typescript I get:

Uncaught Error: Can't find sockjs-client/dist/sockjs.js [undefined] (required by /Volumes/T16-Skyhopper/cxone/libs/cxone-client-platform-services/src/lib/notification/notification.service.ts)

Any thoughts?

izogfif commented 8 months ago

@scottfwalter Sorry, I don't have experience with Karma, can't help. I'd suggest to check those configuration files (tsconfig, angular.json and such) to find out where they're looking for d.ts files. Maybe Karma just can't find them?

TigselemaAlex commented 6 months ago

If you use Angular 17 you need to add this in the main.ts: image

ryanhex53 commented 4 months ago

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';
warjiang commented 4 months ago

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 🎉