ReconnectingWebSocket is not a constructor
See original GitHub issueI am trying to use the reconnecting-websocket
library in a TypeScript project using Node.js module resolution. Compiling works but when starting the project I am always getting the following error:
TypeError: reconnecting_websocket_1.default is not a constructor
This is what my tsconfig.json
looks like:
{
"compilerOptions": {
"lib": [
"dom",
"es6"
],
"module": "commonjs",
"moduleResolution": "node",
"outDir": "dist",
"removeComments": true,
"rootDir": "src",
"strict": true,
"target": "es5"
},
"exclude": [
"dist",
"node_modules"
]
}
And here is my demo code:
import ReconnectingWebSocket, {Options} from 'reconnecting-websocket';
import NodeWebSocket = require('ws');
const options: Options = {
WebSocket: typeof window !== 'undefined' ? WebSocket : NodeWebSocket,
connectionTimeout: 4000,
debug: true,
maxReconnectionDelay: 10000,
maxRetries: Infinity,
minReconnectionDelay: 4000,
reconnectionDelayGrowFactor: 1.3,
};
function buildWebSocketURL(timestamp: number = Date.now()): string {
return `wss://echo.websocket.org/?time=${timestamp}`;
}
const socket: ReconnectingWebSocket = new ReconnectingWebSocket(
buildWebSocketURL,
undefined,
options
);
socket.onmessage = (event: MessageEvent): void => {
console.log(`Response: ${event.data}`);
};
socket.onerror = (): void => {
console.error('Ooops!');
};
socket.onopen = (): void => {
console.log('WebSocket is alive!');
};
You can also try it yourself by cloning my repository and executing yarn start
: https://github.com/bennyn/reconnecting-websocket-node
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Typescript ReconnectingWebSocket fails to call constructor
reconnecting_websocket_1.default is not a constructor. The typescript code is: import ReconnectingWebSocket, { Options } from ...
Read more >october/reconnecting-websocket - npm package
This way you can use this module in cli/testing/node.js or use a decorated/alternative WebSocket. The only requisite is that the given constructor must...
Read more >reconnecting-websocket
3, Licensed under the Apache License, Version 2.0 (the "License"); you may not use. 4, this file except in compliance with the License....
Read more >reconnecting-websocket
WebSocket · WebSocket constructor, if none provided, defaults to global WebSocket ; maxReconnectionDelay · max delay in ms between reconnections.
Read more >reconnecting-websocket
Check Reconnecting-websocket 4.4.0 package - Last release 4.4.0 with MIT licence at ... Node.js, React Native); Dependency free (does not depend on Window, ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
My demo will run when using
"esModuleInterop": true
but this introduces a whole set of new problems.When using
esModuleInterop
, then you cannot extendEventEmitter
and namespace-style imports (such asimport * as logdown from 'logdown'
) will cause a failure at runtime.I extended my example to showcase that: https://github.com/bennyn/reconnecting-websocket-node/commit/71c66c1675cb74d09f6ac784998d3e6f7de8012c
Hi, try setting
esModuleInterop
totrue
intsconfig.json