Skip to content

Public declaration files reference undeclared dependency ws #353

Closed

Description

@heroboy

@hono/node-server exposes ws in its public declaration files, but ws is not declared as a dependency.

For example, dist/index.d.mts contains:

import { WebSocket, WebSocketServer } from "ws";

However, ws is currently only a devDependency.

This causes TypeScript consumers to fail compilation with:

TS2307: Cannot find module 'ws' or its corresponding type declarations.

This is reproducible under pnpm / NodeNext module resolution, but the issue is not pnpm-specific — consumers are not expected to install a package's devDependencies.

Since ws is referenced from public .d.ts/.d.mts files, it is effectively part of the package's public type surface and should probably be declared as one of:

  • dependencies
  • peerDependencies
  • optional peer dependency

Otherwise consumers must manually install ws (and sometimes @types/ws) even if they do not use websocket functionality directly.

Activity

  1. Venerons commented on May 14, 2026

    @Venerons

    Same issue here, typescript compilation fails after a npm ci

  2. heroboy commented on May 21, 2026

    @heroboy
    Author

    One workaround is to set skipLibCheck to true in tsconfig.json.

  3. yusukebe commented on May 23, 2026

    @yusukebe
    Member

    We don't want to add external libraries, including ws and @types/ws, to dependencies and peerDependencies. So, users should set skipLibCheck:true and install ws and @types/ws manually if they want to use the WebSockets feature.

    We left the following description on the README.

    To enable this, install ws (and @types/ws) in your project, then create and provide a WebSocketServer as shown in the example below.

    Hy @BlankParticle! Do you have any other option for this?

  4. BlankParticle commented on May 23, 2026

    @BlankParticle
    Contributor

    Hey, sorry to hear that you guys are having this issue, I develop with skipLibCheck: true so I never ran into this problem.
    But this is a problem that people who are not using websockets are having issues with ws types.

    @yusukebe I think the best way to fix this problem is to not use ws types and replace them with same structural types that confront to ws types, so we would be able to use them without public dependency on @types/ws.
    Basically we need to copy the public facing type of WebSocket and WebSocketServer (https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/ws/index.d.ts)

    Based on the parts we use in our code this is the minimal types we would need, so we can accept WebSocketServer objects from users without knowing the exact types.

    import type { IncomingMessage } from 'node:http'
    import type { Duplex } from 'node:stream'
    
    type WSReadyState = 0 | 1 | 2 | 3;
    
    export type WebSocketData = string | ArrayBuffer | Uint8Array | readonly Uint8Array[]
    
    export type WebSocketSendOptions = {
      compress?: boolean
    }
    
    export interface WebSocketLike {
      protocol: string
      readyState: WSReadyState
      close(code?: number, reason?: string): void
      send(data: string | ArrayBuffer | ArrayBufferView, options?: WebSocketSendOptions): void
      on(event: 'message', listener: (data: WebSocketData, isBinary: boolean) => void): this
      on(event: 'close', listener: (code: number, reason: Uint8Array) => void): this
      on(event: 'error', listener: (error: unknown) => void): this
      off(event: 'message', listener: (data: WebSocketData, isBinary: boolean) => void): this
    }
    
    export interface WebSocketServerLike {
      options: {
        noServer?: boolean
      }
      on(event: 'connection', listener: (ws: WebSocketLike, request: IncomingMessage) => void): this
      on(event: 'headers', listener: (headers: string[]) => void): this
      off(event: 'headers', listener: (headers: string[]) => void): this
      emit(event: 'connection', ws: WebSocketLike, request: IncomingMessage): boolean
      handleUpgrade(
        request: IncomingMessage,
        socket: Duplex,
        head: Buffer,
        callback: (ws: WebSocketLike) => void
      ): void
      close(): void
    }

    I will make the PR if this works

  5. yusukebe commented on May 23, 2026

    @yusukebe
    Member

    @BlankParticle Thanks for the super quick response. Sounds good. Please!

  6. BlankParticle commented on May 23, 2026

    @BlankParticle
    Contributor

    I have made the PR along with a lint rule to prevent this from happening again
    #359

    we can close #358 as we will not expose ws publicly anymore

  7. heroboy commented on May 23, 2026

    @heroboy
    Author

    I have a suggestion: use “subpath exports”, just like Hono itself does. Move everything related to ws into @hono/node-server/ws, and use interface merging to add websocket to Option.

  8. BlankParticle commented on May 23, 2026

    @BlankParticle
    Contributor

    I have a suggestion: use “subpath exports”, just like Hono itself does. Move everything related to ws into @hono/node-server/ws, and use interface merging to add websocket to Option.

    I can't imagine how /ws subpath would be used, but I think its better to just duck type the public api we accept instead of trying to make interface merging work given how fiddly it is to make it work properly

  9. yusukebe commented on May 24, 2026

    @yusukebe
    Member

    I definitely enabled "subpath exports" on hono, but I don't think we need to do the same for @hono/node-server. And the topic is out of scope of this issue, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions