question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ReconnectingWebSocket is not a constructor

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
bennycodecommented, Nov 26, 2018

My demo will run when using "esModuleInterop": true but this introduces a whole set of new problems.

When using esModuleInterop, then you cannot extend EventEmitter and namespace-style imports (such as import * 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

1reaction
pladariacommented, Nov 26, 2018

Hi, try setting esModuleInterop to true in tsconfig.json

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found