socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

Cannot find name XMLHttpRequest in polling-xhr.d.ts

Open Granjow opened this issue 9 months ago • 1 comments

With version 4.8.1 of socket.io-client and Node.js 22, "@types/node": "22.13.5" and "typescript": "^5.7.3", I have a build error:

node_modules/engine.io-client/build/esm/transports/polling-xhr.d.ts:65:58 - error TS2304: Cannot find name 'XMLHttpRequest'.

65     constructor(createRequest: (opts: RequestOptions) => XMLHttpRequest, uri: string, opts: RequestOptions);

Probably related: socketio/socket.io-client#1561

Granjow avatar Feb 27 '25 18:02 Granjow

Hi! I wasn't able to reproduce: https://github.com/socketio/socket.io/tree/main/examples/typescript-example/esm

$ node -v
v22.8.0

$ npm ls typescript
[email protected] /git/socket.io/socket.io/examples/typescript-example/esm
├─┬ [email protected]
│ └── [email protected] deduped
└── [email protected]

$ npm ls @types/node
[email protected] /git/socket.io/socket.io/examples/typescript-example/esm
├─┬ [email protected]
│ └─┬ [email protected]
│   ├─┬ @types/[email protected]
│   │ └── @types/[email protected] deduped
│   └── @types/[email protected] deduped
└─┬ [email protected]
  └── @types/[email protected]

$ npm run build

> [email protected] build
> tsc

Could you please check?

darrachequesne avatar Mar 03 '25 11:03 darrachequesne

XMLHttpRequest is not provided by this library. It is meant to be provided by tsc.

@Granjow Did you have lib: ["dom"] or equalivient in tsconfig.json ?

jokester avatar Jun 27 '25 14:06 jokester

@jokester I have these compiler options:

  "compilerOptions": {
    "composite": true,
    "outDir": "dist",
    "module": "commonjs",
    "noImplicitAny": true,
    "removeComments": false,
    "preserveConstEnums": true,
    "inlineSourceMap": true,
    "declaration": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "target": "es6",
    "strict": true,
    "jsx": "preserve",
    "lib": [
      "es2018",
      "esnext"
    ]
  }

Granjow avatar Jun 27 '25 18:06 Granjow

Have the same issue:

  • node 23
  • typescript 5.8

tsconfig:

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "compilerOptions": {
    "strict": true,
    "strictNullChecks": true,
    "skipLibCheck": false,
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "allowSyntheticDefaultImports": true,
    "noImplicitAny": true,
    "forceConsistentCasingInFileNames": true,
    "target": "es2022",
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "declaration": true,
    "lib": ["es2022"],
    "outDir": "dist",
    "types": ["node"]
  },
  "include": ["source/**/*"],
  "exclude": ["node_modules", "dist"]
}
../../node_modules/engine.io-client/build/esm/transports/polling-xhr.d.ts:65:58 - error TS2304: Cannot find name 'XMLHttpRequest'.

65     constructor(createRequest: (opts: RequestOptions) => XMLHttpRequest, uri: string, opts: RequestOptions);
                                                            ~~~~~~~~~~~~~~


Found 1 error in ../../node_modules/engine.io-client/build/esm/transports/polling-xhr.d.ts:65

It seems that socket.io-client doesn't want to run on base Node/TS. It's a typescript bug in this library.

Did you have lib: ["dom"] or equalivient in tsconfig.json ?

DOM fixes the issue, however it should not be required for a library that is compatible with NodeJS. XMLHttpRequest is not relevant for Node and developers shouldn't be expected to add the DOM types to get this to work, in my opinion.

perry-mitchell avatar Aug 10 '25 17:08 perry-mitchell

@perry-mitchell That's totally reasonable.

I may have a fix. Can any of you try adding this to local polling-xhr.d.ts (under node_modules, cjs or esm depends on your usage), ensure tsconfig doesn't have lib: ['dom'] , and see if the type error persists?

/**
 * A dummy interface to satisfy TypeScript's type checking.
 * When real XHR is avaiable, this do nothing.
 * Otherwise this is a placeholder type to silent tsc.
 */
interface XMLHttpRequest { }

jokester avatar Aug 11 '25 04:08 jokester

Hi @jokester - thanks for the quick response.

I've already switched to using Rollup to bundle the backend of this particular project - it handles the needs of socket.io-client just fine, it seems. I'm not sure what it does under the hood - it may well provide a XHR-compatible client which squelches the error. So for now I'll probably just stick with this, thanks.

perry-mitchell avatar Aug 11 '25 05:08 perry-mitchell