Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Types are not installed #4621

Closed
Bazhan4ik opened this issue Feb 1, 2023 · 22 comments
Closed

Types are not installed #4621

Bazhan4ik opened this issue Feb 1, 2023 · 22 comments

Comments

@Bazhan4ik
Copy link

Describe the bug
Types for socket.io-parser and engine.io-parser are not installed

To Reproduce
Socket.IO server version: 4.5.4

When compile with typescript it throws these errors:

TS7016: Could not find a declaration file for module 'engine.io-parser'. '.../node_modules/engine.io-parser/build/cjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/engine.io-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'engine.io-parser';`

3 import * as parser from "engine.io-parser";
                          ~~~~~~~~~~~~~~~~~~

TS7016: Could not find a declaration file for module 'engine.io-parser'. '.../node_modules/engine.io-parser/build/cjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/engine.io-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'engine.io-parser';`

4 import { Packet } from "engine.io-parser";
                         ~~~~~~~~~~~~~~~~~~

TS7016: Could not find a declaration file for module 'socket.io-parser'. '.../node_modules/socket.io-parser/build/cjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/socket.io-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'socket.io-parser';`

2 import { Packet } from "socket.io-parser";
                         ~~~~~~~~~~~~~~~~~~

TS7016: Could not find a declaration file for module 'socket.io-parser'. '.../node_modules/socket.io-parser/build/cjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/socket.io-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'socket.io-parser';`

10 import * as parser from "socket.io-parser";
                           ~~~~~~~~~~~~~~~~~~

TS7016: Could not find a declaration file for module 'socket.io-parser'. '.../node_modules/socket.io-parser/build/cjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/socket.io-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'socket.io-parser';`

11 import type { Encoder } from "socket.io-parser";
                                ~~~~~~~~~~~~~~~~~~

TS7016: Could not find a declaration file for module 'socket.io-parser'. '.../node_modules/socket.io-parser/build/cjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/socket.io-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'socket.io-parser';`

3 import { Packet } from "socket.io-parser";
                         ~~~~~~~~~~~~~~~~~~

Did I do something wrong?
Do I have to install the types manually?

@Bazhan4ik Bazhan4ik added the to triage Waiting to be triaged by a member of the team label Feb 1, 2023
@darrachequesne
Copy link
Member

Hi! That's weird, the types should be included in the package.

Which OS are you using? Which version of typescript?

@Bazhan4ik
Copy link
Author

Hi

I'm on Windows 11, and I'm using verion ^4.9.4 of typescript

@Bazhan4ik
Copy link
Author

Bazhan4ik commented Feb 4, 2023

This is probably because of typescript. This is my tsconfig.json, and I set module: NodeNext which I never used before.

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
  },
  "exclude": [
    "node_modules",
    "web/node_modules",
  ]
}

I tried to set module to commonjs and I get no errors.

@algnor
Copy link

algnor commented Feb 6, 2023

I'm running into the same issue, using commonjs fixes the problem, however a few of the packages i need are pure esm only, so changing to commonjs isn't an alternative.
Here is the log.

#0 0.436 > tsc -p api/tsconfig.json && cp -r ./api/src/resources/ ./dist/api/resources/
node_modules/engine.io/build/engine.io.d.ts:3:25 - error TS7016: Could not find a declaration file for module 'engine.io-parser'. '/home/algnor/Fourcorner/4corner/node_modules/engine.io-parser/build/cjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/engine.io-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'engine.io-parser';`

3 import * as parser from "engine.io-parser";
                          ~~~~~~~~~~~~~~~~~~

node_modules/engine.io/build/transport.d.ts:4:24 - error TS7016: Could not find a declaration file for module 'engine.io-parser'. '/home/algnor/Fourcorner/4corner/node_modules/engine.io-parser/build/cjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/engine.io-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'engine.io-parser';`

4 import { Packet } from "engine.io-parser";
                         ~~~~~~~~~~~~~~~~~~

node_modules/socket.io/dist/client.d.ts:2:24 - error TS7016: Could not find a declaration file for module 'socket.io-parser'. '/home/algnor/Fourcorner/4corner/node_modules/socket.io-parser/build/cjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/socket.io-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'socket.io-parser';`

2 import { Packet } from "socket.io-parser";
                         ~~~~~~~~~~~~~~~~~~

node_modules/socket.io/dist/index.d.ts:10:25 - error TS7016: Could not find a declaration file for module 'socket.io-parser'. '/home/algnor/Fourcorner/4corner/node_modules/socket.io-parser/build/cjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/socket.io-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'socket.io-parser';`

10 import * as parser from "socket.io-parser";
                           ~~~~~~~~~~~~~~~~~~

node_modules/socket.io/dist/index.d.ts:11:30 - error TS7016: Could not find a declaration file for module 'socket.io-parser'. '/home/algnor/Fourcorner/4corner/node_modules/socket.io-parser/build/cjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/socket.io-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'socket.io-parser';`

11 import type { Encoder } from "socket.io-parser";
                                ~~~~~~~~~~~~~~~~~~

node_modules/socket.io/dist/socket.d.ts:3:24 - error TS7016: Could not find a declaration file for module 'socket.io-parser'. '/home/algnor/Fourcorner/4corner/node_modules/socket.io-parser/build/cjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/socket.io-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'socket.io-parser';`

3 import { Packet } from "socket.io-parser";
                         ~~~~~~~~~~~~~~~~~~


Found 6 errors in 5 files.

Errors  Files
     1  node_modules/engine.io/build/engine.io.d.ts:3
     1  node_modules/engine.io/build/transport.d.ts:4
     1  node_modules/socket.io/dist/client.d.ts:2
     2  node_modules/socket.io/dist/index.d.ts:10
     1  node_modules/socket.io/dist/socket.d.ts:3

and tsconfig.json

{
  "compilerOptions": {
    "outDir": "../dist/api/",
    "noImplicitAny": true,
    "module": "NodeNext",
    "target": "ES2020",
    "allowJs": true,
    "moduleResolution": "NodeNext",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "sourceMap": true
  }
}

@darrachequesne
Copy link
Member

The types are present though (in types": "./build/esm/index.d.ts"), but it seems the nodeNext module resolution does not find them. Not sure about what we could do here... 👀

@darrachequesne darrachequesne added needs investigation and removed to triage Waiting to be triaged by a member of the team labels Feb 8, 2023
@jcjp
Copy link

jcjp commented Feb 9, 2023

I am on the latest socket.io and @socket.io/cluster-adapter is this error related? When passing createAdapter from @socket.io/cluster-adapter to socket.io server.adapter? This is the error:

Argument of type '(nsp: any) => ClusterAdapter' is not assignable to parameter of type 'AdapterConstructor'.
Type '(nsp: any) => ClusterAdapter' is not assignable to type '(nsp: Namespace<DefaultEventsMap, DefaultEventsMap, DefaultEventsMap, any>) => Adapter'.
Type 'ClusterAdapter' is missing the following properties from type 'Adapter': _encode, persistSession, restoreSession

Seems like this bug has re-occur: #3796.

@igorls
Copy link
Contributor

igorls commented Feb 9, 2023

Just submitted PRs to fix this on socket.io and engine.io

Tested with the following config:

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "target": "ESNext",
    "sourceMap": true,
    "outDir": "dist",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  },
  "include": [
    "src/**/*"
  ],
  "exclude": [
    "node_modules"
  ]
}

@javaarchive
Copy link

I was able to downgrade to 4.5.4 to solve the issue temporarily since I was unable to install the above PR since installing from github excludes the built "dist" directory.

darrachequesne pushed a commit to socketio/engine.io that referenced this issue Feb 17, 2023
darrachequesne pushed a commit that referenced this issue Feb 20, 2023
The import added in [1] was invalid, because it used an non-exported
class.

Related: #4621

[1]: d4a9b2c
@darrachequesne
Copy link
Member

This should be fixed by d0b22c6, included in version 4.6.1.

@igorls thanks a lot for your work on this.

Please reopen if needed.

@darrachequesne darrachequesne added this to the 4.6.1 milestone Feb 20, 2023
@Metapyziks
Copy link

Is there an extra step I need to take besides installing 4.6.1? It seems like I have the same problem still:

> npm list socket.io
[email protected] C:\Users\metap\source\repos\bge\bge-dev
└── [email protected]

> tsc

node_modules/engine.io/build/engine.io.d.ts:3:25 - error TS7016: Could not find a declaration file for module 'engine.io-parser'. 'C:/Users/metap/source/repos/bge/bge-dev/node_modules/engine.io-parser/build/cjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/engine.io-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'engine.io-parser';`

3 import * as parser from "engine.io-parser";
                          ~~~~~~~~~~~~~~~~~~

node_modules/engine.io/build/socket.d.ts:5:25 - error TS7016: Could not find a declaration file for module 'engine.io-parser'. 'C:/Users/metap/source/repos/bge/bge-dev/node_modules/engine.io-parser/build/cjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/engine.io-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'engine.io-parser';`

5 import { RawData } from "engine.io-parser";
                          ~~~~~~~~~~~~~~~~~~

node_modules/engine.io/build/transport.d.ts:4:24 - error TS7016: Could not find a declaration file for module 'engine.io-parser'. 'C:/Users/metap/source/repos/bge/bge-dev/node_modules/engine.io-parser/build/cjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/engine.io-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'engine.io-parser';`

4 import { Packet } from "engine.io-parser";
                         ~~~~~~~~~~~~~~~~~~

node_modules/socket.io/dist/client.d.ts:2:24 - error TS7016: Could not find a declaration file for module 'socket.io-parser'. 'C:/Users/metap/source/repos/bge/bge-dev/node_modules/socket.io-parser/build/cjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/socket.io-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'socket.io-parser';`

2 import { Packet } from "socket.io-parser";
                         ~~~~~~~~~~~~~~~~~~

node_modules/socket.io/dist/index.d.ts:10:25 - error TS7016: Could not find a declaration file for module 'socket.io-parser'. 'C:/Users/metap/source/repos/bge/bge-dev/node_modules/socket.io-parser/build/cjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/socket.io-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'socket.io-parser';`

10 import * as parser from "socket.io-parser";
                           ~~~~~~~~~~~~~~~~~~

node_modules/socket.io/dist/index.d.ts:11:30 - error TS7016: Could not find a declaration file for module 'socket.io-parser'. 'C:/Users/metap/source/repos/bge/bge-dev/node_modules/socket.io-parser/build/cjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/socket.io-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'socket.io-parser';`

11 import type { Encoder } from "socket.io-parser";
                                ~~~~~~~~~~~~~~~~~~

node_modules/socket.io/dist/socket.d.ts:3:24 - error TS7016: Could not find a declaration file for module 'socket.io-parser'. 'C:/Users/metap/source/repos/bge/bge-dev/node_modules/socket.io-parser/build/cjs/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/socket.io-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'socket.io-parser';`

3 import { Packet } from "socket.io-parser";
                         ~~~~~~~~~~~~~~~~~~


Found 7 errors in 6 files.

Errors  Files
     1  node_modules/engine.io/build/engine.io.d.ts:3
     1  node_modules/engine.io/build/socket.d.ts:5
     1  node_modules/engine.io/build/transport.d.ts:4
     1  node_modules/socket.io/dist/client.d.ts:2
     2  node_modules/socket.io/dist/index.d.ts:10
     1  node_modules/socket.io/dist/socket.d.ts:3

tsconfig.json

{
  "compilerOptions": {
    "target": "es2022",
    "lib": [ "es2022", "DOM" ],
    "module": "es2022",
    "rootDir": "src",
    "allowJs": false,
    "outDir": "dist",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "allowSyntheticDefaultImports": true,
    "noImplicitAny": true,
    "moduleResolution": "node16"
  }
}

@jcjp
Copy link

jcjp commented Mar 23, 2023

Yeah on version 4.6.1 still getting the same error is there something wrong with our implementation of running a client websocket?:

 npm list socket.io
<app_directory>
`-- socket.io@4.6.1
#0 12.97 app/Services/Ws.ts:25:23 - error TS2345: Argument of type '(nsp: any) => ClusterAdapter' is not assignable to parameter of type 'AdapterConstructor'.
#0 12.97   Type '(nsp: any) => ClusterAdapter' is not assignable to type '(nsp: Namespace<DefaultEventsMap, DefaultEventsMap, DefaultEventsMap, any>) => Adapter'.
#0 12.97     Type 'ClusterAdapter' is missing the following properties from type 'Adapter': _encode, persistSession, restoreSession
#0 12.97
#0 12.97 25       this.srv.adapter(createAdapter())
#0 12.97                          ~~~~~~~~~~~~~~~
#0 12.97
#0 12.97
#0 12.97 Cannot complete the build process as there are typescript errors. Use "--ignore-ts-errors" flag to ignore Typescript errors
#0 12.97 [ info ]  cleaning up "./build" directory
// socket.ts
import {Server} from 'socket.io'
import {createAdapter} from '@socket.io/cluster-adapter'
import {setupWorker} from '@socket.io/sticky'
...

class ClientWS {
  public srv: Server

  public boot() {
    ...
    this.srv = new Server(AdonisServer.instance!, {
      path: <path>,
      cors: <cors>
    })

    this.srv.adapter(createAdapter())
    setupWorker(this.srv)
  }
}

export default new ClientWS()

@darrachequesne
Copy link
Member

@maikelRAOW
Copy link

maikelRAOW commented Apr 4, 2023

I too still have the exact same problem as @Metapyziks on version 4.6.1.

@leviathanbadger
Copy link

leviathanbadger commented Apr 6, 2023

I have a slightly different error as @Metapyziks, but I'm pretty sure it's the same root cause: actually on closer inspection it was the same, just formatted differently. Still, here are some more technical details to help diagnose the issue.

This is my tsconfig.json:

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "compilerOptions": {
    "outDir": "dist",
    "removeComments": true,
    "target": "ES6",
    "strict": true,
    "sourceMap": true,
    "module": "Node16",
    "moduleResolution": "node16",
    "forceConsistentCasingInFileNames": true,
    "experimentalDecorators": true
  },
  "include": ["./src/**/*.ts"],
  "exclude": [
    "node_modules/**/*"
  ]
}

Node version v18.15.0
NPM version 9.5.0
Typescript version 5.0.3
Socket.io version 4.6.1

haneenmahd pushed a commit to haneenmahd/socket.io that referenced this issue Apr 15, 2023
The import added in [1] was invalid, because it used an non-exported
class.

Related: socketio#4621

[1]: socketio@d4a9b2c
@brettz9
Copy link

brettz9 commented May 5, 2023

I see there have been some fix commits--can these be published if they resolve the issue?

@kettanaito
Copy link

kettanaito commented May 17, 2023

The type definitions for the CommonJS target of this library seem to be broken (as of [email protected]):

Screenshot 2023-05-17 at 20 12 31

There are no type definitions in the /cjs/ directory, which is a mistake. Any dependency resolving socket.io-parser as CommonJS will get no type definitions, and the entire package will be annotated as any.

Wait, but we are setting a single "types": "./build/esm/index.d.ts" in the root-level package.json, isn't that enough? No, it's not enough. Old bundlers that don't understand the exports conditions will load the CommonJS path at ./build/cjs/index.js and will attempt to lookup type definitions relatively to that entrypoint. This is also the behavior on some modern bundlers as well.

How to fix this?

You have two options to fix this:

Option 1: Reference /esm/ types from cjs/package.json

This is a great option if the build output is identical between the CJS and ESM targets of this library. If it is, simply adding { "types": "../esm/index.d.ts" } to /cjs/package.json will solve the problem and everyone will be happy.

Option 2: Mirror type definitions in /cjs/

If there's any concern that the build outputs may not be identical, consider emitting another set of *.d.ts files alongside the /cjs/ build.

I can help with either solutions just let me know which one you prefer.

@darrachequesne
Copy link
Member

@kettanaito thanks for the detailed analysis 👍

If it is, simply adding { "types": "../esm/index.d.ts" } to /cjs/package.json will solve the problem and everyone will be happy.

So if I'm understanding correctly, the bundler does understand { "types": "../esm/index.d.ts" } in ./build/cjs/package.json but not { "types": "./build/esm/index.d.ts" } in ./package.json, is that correct?

This is also the behavior on some modern bundlers as well.

Out of curiosity, which bundler has this behavior?

@kettanaito
Copy link

@darrachequesne, yes, you are correct. At least, this is the behavior I'm getting using TypeScript 5.0 in some of my open-source projects. I've gone through an extensive implementation of ESM+TS recently for some of my projects and know firsthand how painful it is to get this right for every consumer.

@24thsaint
Copy link

Hello, I am also experiencing this now on our CI build.

node_modules/@types/socket.io/index.d.ts(20,68): error TS7016: Could not find a declaration file for module 'socket.io-parser'. '/home/dev_ops/builds/mSJ5VfCs/0/XXX/YYY/node_modules/socket.io-parser/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/socket.io-parser` if it exists or add a new declaration (.d.ts) file containing `declare module 'socket.io-parser';`

My devDependencies have:

"@types/socket.io-parser": "^3.0.0",

And tsconfig.json:

{
  "compilerOptions": {
    "target": "es2018",
    "module": "commonjs",
    "outDir": "./lib",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true
  },
  "exclude": [
    "test",
    "client",
    "uploads"
  ],
  "ts-node": {
    "files": true
  },
  "files": [
    "index.d.ts"
  ]
}

@darrachequesne
Copy link
Member

darrachequesne commented May 24, 2023

@kettanaito we went for option 2 in socketio/socket.io-parser@dcc70d9, included in [email protected]. Could you please check if that fixes the issue?

Update: engine.io-parser was updated too: socketio/engine.io-parser@e4a1c2b

@24thsaint the types are now included in the package, there should be no need for the @types/.. installation. Which version of typescript / socket.io are you using?

@24thsaint
Copy link

Hi @darrachequesne ,

It's a peer dependency coming from:

"@feathersjs/socketio": "^4.5.13",

Looks like it's using:

"socket.io": "^4.6.1"

I'm on:

"typescript": "^4.5.5"

darrachequesne added a commit to socketio/engine.io-parser that referenced this issue May 24, 2023
dzad pushed a commit to dzad/socket.io that referenced this issue May 29, 2023
The import added in [1] was invalid, because it used an non-exported
class.

Related: socketio#4621

[1]: socketio@6652a3f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests