Marc Rasmussen
Marc Rasmussen

Reputation: 20545

socket.io client with TypeScript

I am trying to use TypeScript with socket.io in my client:

import { Manager, Socket} from "socket.io-client";
import {DefaultEventsMap} from "@socket.io/component-emitter";

export class SocketIoService {
    private socket: Socket<DefaultEventsMap, DefaultEventsMap>

    constructor() {
        const manager = new Manager("http://localhost:3000");
        this.socket = manager.socket("/");
    }

    public connect() {
        this.socket.emit("hello");
        this.socket.on("noArg", () => {
            // ...
        });

        this.socket.on("basicEmit", (a, b, c) => {
            // a is inferred as number, b as string and c as buffer
            console.log(a + b + c);
        });
    }
}

The problem is when I start my server I get the following error:

The requested module './../../../parseuri/index.js' does not provide an export named 'default'

What have I done wrong?

Upvotes: 1

Views: 2865

Answers (3)

Dreamy Player
Dreamy Player

Reputation: 615

Server:

import { Server } from "socket.io";

const io = new Server(httpServer);

Client:

import { Manager } from "socket.io-client";

const manager = new Manager("http://localhost:3000");
const socket = manager.socket("/");

Upvotes: 3

dimiguel
dimiguel

Reputation: 1549

I'm not entirely sure how the error is related to this specific code. I just created an empty folder, added your code in, and with an empty tsconfig.json was able to compile. Here's the compiled code:

"use strict";
exports.__esModule = true;
exports.SocketIoService = void 0;
var socket_io_client_1 = require("socket.io-client");
var SocketIoService = /** @class */ (function () {
    function SocketIoService() {
        var manager = new socket_io_client_1.Manager("http://localhost:3000");
        this.socket = manager.socket("/");
    }
    SocketIoService.prototype.connect = function () {
        this.socket.emit("hello");
        this.socket.on("noArg", function () {
            // ...
        });
        this.socket.on("basicEmit", function (a, b, c) {
            // a is inferred as number, b as string and c as buffer
            console.log(a + b + c);
        });
    };
    return SocketIoService;
}());
exports.SocketIoService = SocketIoService;

I'd suggest doing:

$ rm -rf node_modules
$ npm i

And trying again.

Upvotes: 0

chandresh_n
chandresh_n

Reputation: 473

(this should have been a comment but I do not have enough rep to comment)

The error signifies that you are trying to default import from a module which has named exports.

Can you attach the stacktrace of the error?

I am interested in the module which triggers the import to './../../../parseuri/index.js'.

Upvotes: 1

Related Questions