ws: importing the module doesn't work like shown in the readme for TypeScript projects

  • I’ve searched for any related issues and avoided creating a duplicate issue.

Description

The module must be imported with import * as WebSocket from 'ws'; while the readme shows the module being imported with import WebSocket, { WebSocketServer } from 'ws';

Reproducible in:

Any TypeScript project.

Steps to reproduce:

  1. install ws

  2. import it like import WebSocket, { WebSocketServer } from 'ws'; in a project

  3. try using the module as expected

Expected result:

The module can be imported as shown in the readme.

Actual result:

The module must be imported in alternative ways.

Attachments:

Just try running this piece of code

import WebSocket, { WebSocketServer } from 'ws';

const WSServer = new WebSocketServer();

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 26 (13 by maintainers)

Commits related to this issue

Most upvoted comments

This is a TypeScript issue. Remove TypeScript and it works.

We do not officially support TypeScript so it’s up to TypeScript users to fix TypeScript issues.

The fix should be done in the TypeScript type definitions and not in ws documentation.

@tamuratak I think that Typescript support is necessary nowadays.

I suspect there is a problem with CJS and ESM versions having different interfaces due to the run-time shape being different.

import ws from 'ws';
ws.Server; // This doesn't exist.
ws.WebSocketServer; // This doesn't exist either.
const ws = require('ws');
ws.Server; // This exists

I’m not sure if typescript definitions support having different definitions for CJS and ESM.

I’m closing this as https://github.com/DefinitelyTyped/DefinitelyTyped/pull/55151 should fix the issue once merged.

Fair enough. I agree that it is a good practice for OSS maintainers to decrease the maintenance burden as much as possible. I have written your comment in the PR. I want to wait for the @types/ws maintainers’ reply.