Recently, I attempted to implement web sockets using socket.io in a Node server written in TypeScript with ExpressJS and bundled with Webpack.
The server code is structured as follows:
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 issue arises from the last line of the server code snippet.
Here's the corresponding 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'
}
}
];
However, during the webpack compilation process, several warnings are generated:
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
....
Upon running the server script, the following error is encountered:
fs.js:549 return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
TypeError: path must be a string
....
If anyone has insights on what could potentially be causing these errors, please feel free to share your thoughts!