Closed artuska closed 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.
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?
@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 :(
@artuska do you have some solution of this problem?
@cawena just define it yourself before other js script files, eg. in head
section:
<script>
var global = window;
</script>
is there a proper fix planned to be added ?
any updates?
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
}
Why not just use globalThis?
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';
@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?
@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?
If you use Angular 17 you need to add this in the main.ts:
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 🎉
What is
global
and why it is not defined?node -v v10.0.0 npm -v 6.1.0 sockjs-client 1.1.4