Matthias
Matthias

Reputation: 1215

Runtime error using socket.io in typescript bundled by webpack

I was trying to make use of web-sockets via socket.io in a typescript written node server with expressjs. Bundled with webpack.

The server code Looks as follow:

import * as Express from "express";
import * as SocketIO from "socket.io";
import * as Http from "http";

const app = Express();

app.get("/", (reqest: Express.Request, response: Express.Response) => {
    response.sendFile(process.cwd() + "/dist/index.html");
});

app.use(Express.static("./dist/"));

const server = app.listen(3210, () => {
    console.log("Listening to port 3210");
});

const io = SocketIO.listen(server);

The last line causes the issues mentioned later.

The coresponding webpack.config.js:

module.exports = [
    {
        entry: "./server/main.ts",
        output: {
            path: "./dist",
            filename: "server.js"
        },
        debug: true,
        devtool: "source-map",
        module: {
            loaders: [
                {
                    test: /\.ts$/,
                    loader: "ts-loader"
                }
                , {
                    test: /\.json/,
                    loader: "json-loader"
                }
            ]
        },
        target: "node",
        node: {
            fs: "empty",
            net: "empty"
        }
        ts: {
            configFileName: 'server.tsconfig.json'
        }
    }
];

But at the end webpack prints out the following warnings:

WARNING in ./~/express/lib/view.js

Critical dependencies:

78:29-56 the request of a dependency is an expression

@ ./~/express/lib/view.js 78:29-56

WARNING in ./~/socket.io/~/engine.io/lib/server.js

Critical dependencies:

75:43-65 the request of a dependency is an expression

@ ./~/socket.io/~/engine.io/lib/server.js 75:43-65

WARNING in ./~/socket.io/~/engine.io/~/ws/lib/Validation.js

Module not found: Error: Cannot resolve module 'utf-8-validate' in ...\node_modules\socket.io\node_modules\engine.io\node_modules\ws\lib

@ ./~/socket.io/~/engine.io/~/ws/lib/Validation.js 10:19-44

WARNING in ./~/socket.io/~/engine.io/~/ws/lib/BufferUtil.js

Module not found: Error: Cannot resolve module 'bufferutil' in ...\node_modules\socket.io\node_modules\engine.io\node_modules\ws\lib

@ ./~/socket.io/~/engine.io/~/ws/lib/BufferUtil.js 10:19-40

WARNING in ./~/socket.io/~/socket.io-client/~/engine.io-client/~/ws/lib/Validation.js

Module not found: Error: Cannot resolve module 'utf-8-validate' in ...\node_modules\socket.io\node_modules\socket.io-client\node_modules\engine.io-client\node_modules\ws\lib

@ ./~/socket.io/~/socket.io-client/~/engine.io-client/~/ws/lib/Validation.js 10:19-44

WARNING in ./~/socket.io/~/socket.io-client/~/engine.io-client/~/ws/lib/BufferUtil.js

Module not found: Error: Cannot resolve module 'bufferutil' in ...\node_modules\socket.io\node_modules\socket.io-client\node_modules\engine.io-client\node_modules\ws\lib

@ ./~/socket.io/~/socket.io-client/~/engine.io-client/~/ws/lib/BufferUtil.js 10:19-40

Process terminated with code 0.

Running server.js results in:

fs.js:549 return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);

TypeError: path must be a string

....

Does anyone know what I did wrong?

Upvotes: 1

Views: 2182

Answers (1)

Dorian Tudorache
Dorian Tudorache

Reputation: 59

import * as SocketIO from "socket.io";

should be:

import SocketIO from "socket.io-client";

Upvotes: 0

Related Questions