But it didn’t help much as the import syntax seems to be fine.
I identified that the socket.io-client package is "type": "commonjs", but I’m not sure what else should I do to make it work besides changing the way to import it on my Cocos Typescript component.
import "socket.io-client" imports the main module of package socket.io-client which is dedicated to Node.js – requires some Node.js builtin packages to work.
To use it in Creator, you should use the WEB version:
import io from 'socket.io-client/dist/socket.io.js';
To eliminate the type error, new a some-name.d.ts in project:
// Declare the shape of "socket.io-client/dist/socket.io.js"
declare "socket.io-client/dist/socket.io.js" {
import io from "socket.io-client";
export default io;
}
import io from 'socket.io-client/dist/socket.io.js';
Did solve the problem, after some extra configuration needed by SocketIO about CORS I was able to connect and receive the socket.id successfully.
Now I’ll study SocketIO to see how to implement things.
But to eliminate the type error I needed to change my some-name.d.ts a little from what you suggested to:
// Declare the shape of "socket.io-client/dist/socket.io.js"
declare module "socket.io-client/dist/socket.io.js" {
import { io } from "socket.io-client";
export default io;
}
I’m not sure if these little changes to add module and { io } makes any problems, but now VSCode doesn’t warn me about anything and the project and connection seems to be working fine.
I’m not sure if these little changes to add module and { io } makes any problems
You’re right. socket.io-client exports io as named export instead of default export. By looking at node_modules\socket.io-client\build\index.d.ts, I think it’d better to re-export all named exports from "socket.io-client/dist/socket.io.js":
declare module "socket.io-client/dist/socket.io.js" {
export * from "socket.io-client";
}