socket.io
socket.io copied to clipboard
Missing dependency when importing socket.io-client in TypeScript
Thanks for your great library, it's a real life saver for me.
Describe the bug I struggle with missing dependencies.
To Reproduce
I've created a new project and added the dependency socket.io-client.
My client code in app.ts (for the browser) looks like this:
import { io, Socket } from "socket.io-client";
tsconfig.js:
{
"compilerOptions": {
"target": "ES2022", // Choose a modern target, such as ES6 or later
"module": "ES2022", // Use ES6 module system (import/export)
"outDir": "./dist", // Specify output directory
"rootDir": "./ts-src", // Specify root directory of your TypeScript files
"strict": true, // Enable strict type checking
"esModuleInterop": true, // Allow compatibility with CommonJS modules
"moduleResolution": "Bundler"
},
"include": ["ts-src/**/*.ts"],
"exclude": ["node_modules"]
}
When I run tsc, I get this error:
node_modules/.pnpm/[email protected]/node_modules/engine.io-client/build/esm/transports/websocket.node.d.ts:3:27 - error TS7016: Could not find a declaration file for module 'ws'. '/home/adigulla/git/minto-editor-prototypen/ui-prototyp-2/node_modules/.pnpm/[email protected]/node_modules/ws/wrapper.mjs' implicitly has an 'any' type.
Try `npm i --save-dev @types/ws` if it exists or add a new declaration (.d.ts) file containing `declare module 'ws';`
3 import { WebSocket } from "ws";
Note: I'm compiling plain JavaScript modules without a bundler. Also, there is a bug in the TypeScript compiler here; line 3 is a comment. The import is in line 12 and the line looks completely different.
Adding "@types/ws": "^8.5.12" to package.json fixes the error.
Expected behavior
Please add information to https://socket.io/docs/v4/typescript/ how to properly set up SocketIO (i.e. which packages do I have to install in addition to socket.io-client) or make sure that all necessary dependencies are installed automatically.
Platform: All
Additional context Node.js v21.7.1 pnpm 9.11.0 "typescript": "^5.6.2" "socket.io-client": "^4.8.0"