socket.io: Cannot find name 'TransformStream' after updating to v5.2.0
Since I updated socket.io from the v4.7.1 to the v4.7.2 (and so engine.io-parser from the v5.1.0 to the v5.2.1), I’m getting the following errors while building my projects:
node_modules/engine.io-parser/build/cjs/index.d.ts:6:54 - error TS2304: Cannot find name 'TransformStream'.
6 export declare function createPacketEncoderStream(): TransformStream<Packet, any>;
~~~~~~~~~~~~~~~
node_modules/engine.io-parser/build/cjs/index.d.ts:7:96 - error TS2304: Cannot find name 'TransformStream'.
7 export declare function createPacketDecoderStream(maxPayload: number, binaryType: BinaryType): TransformStream<Uint8Array, any>;
~~~~~~~~~~~~~~~
Found 2 errors in the same file, starting at: node_modules/engine.io-parser/build/cjs/index.d.ts:6
Funnily, I’m only getting this error when compiling using tsc but not when compiling using esbuild.
npx tsc -> error
npx esbuild src/index.ts --platform=node -> ok
Here is my tsconfig.json:
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist",
"strict": true,
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"moduleResolution": "node16",
"importHelpers": true,
"target": "es2022",
"module": "node16",
"lib": [
"es2022"
]
},
"include": [
"src/**/*.ts"
]
}
Reading nodejs’s documentation, when using TransformStream, it looks like they are importing it with
import { TransformStream } from 'node:stream/web';
which is not the case for engine.io-parser, so maybe that’s the problem.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 4
- Comments: 24 (10 by maintainers)
Commits related to this issue
- fix(typescript): properly import the TransformStream type When compiling with TypeScript with module set to "node16" and moduleResolution to "node16", the following error would be thrown: > node_mod... — committed to socketio/engine.io-parser by darrachequesne 3 years ago
- fix(engine.io-parser): do not expose the TransformStream type The previous commit [1] tried to work around the fact that the TransformStream object is not exposed in the global scope in the `@types/n... — committed to socketio/socket.io by darrachequesne 2 years ago
@darrachequesne
The latest change messes up typing for client libraries as the new import
import type { TransformStream } from "node:stream/web"pollutes the types with@type/nodeintroduced here: https://github.com/socketio/engine.io-parser/commit/0305b4a7a597e0f070ce8ea17106121f9ab369bc
in tsconfig.json we specify that we do not want any node types:
the following code fails to typecheck if
engine.io-parseris importedremoving the first line fixes the issue, this effects downstream so importing
socket.io-clienthas the same effectThanks for the response, I was inkling towards option 1 anyway, but I wasn’t sure how it would work. Tried it out, seems to work fine now!
It does work with Node.js 16 (down to Node.js 10.2.0 actually), but the TypeScript compiler will report an error if
@types/node@14is used.As a workaround, one can either:
@types/nodeto16.x.ysocket.io-client@4.6.x(as theTransformStreamis used in order to implement theWebTransportlow-level transport, added in4.7.0)Argh, it seems the types from
@types/nodeare out of date,TransformStreamis now available as a global object.We could add
import { TransformStream } from "node:stream/web";, but this would break users with a Node.js version inferior to16.5.0(at runtime).Let me check.
See https://github.com/DefinitelyTyped/DefinitelyTyped/pull/66710
upgrading @types/node to v20.11.30 fix for me angular: 16.2.12 typescript: 4.9.3 socket.io-client: 4.7.5
That’s a great question and unfortunately one I cannot answer since I joined the team long after the UI codebase was established. Ultimately the short answer is the packages are not being updated regularly (mainly because the company limits the access to the outside
npmregistries). I have told the team a few times to update the packages to avoid issues such as this but the codebase is so fragile that if we were to do this we would break almost everything so we are stuck using the old packages for the time being. The workaround I provided earlier seems to hold for now but I wouldn’t consider that the long-term solution if I am being honest. Ultimately it may come down to moving up to a more supported version of Node. Let me know if you have any questions! I appreciate your feedback!unfortunately, I can’t provide examples for reproducing the build failure on the CI/CD config (it’s kept behind a very secret vault away from developers at my company for some reason) however, the version of
@types/nodeis12.11.1. For now, our principal engineer has put a workaround inside thepackage.jsonfile as the following to avoid using the latest version (for now). Hope this helps 😄