Server.prototype.serveClient does not work with webpack
See original GitHub issue- report a bug
Current behaviour
The function uses relative path to check for socket.io-client. However, if I use webpack to bundle a nodejs application that uses webpack. This will become the relative path to socket.io-client in node_modules.
Running node will then return an error:
Error: Cannot find module “.”
Steps to reproduce (if the current behaviour is a bug)
Note: the best way to get a quick answer is to provide a failing test case, by forking the following fiddle for example.
The compiled code in the bundle becomes:
Server.prototype.serveClient = function(v){
if (!arguments.length) return this._serveClient;
this._serveClient = v;
var resolvePath = function(file){
var filepath = path.resolve(__dirname, './../../', file);
if (exists(filepath)) {
return filepath;
}
return /*require.resolve*/(!(function webpackMissingModule() { var e = new Error("Cannot find module \".\""); e.code = 'MODULE_NOT_FOUND'; throw e; }()));
};
if (v && !clientSource) {
clientSource = read(resolvePath( 'socket.io-client/dist/socket.io.js'), 'utf-8');
try {
clientSourceMap = read(resolvePath( 'socket.io-client/dist/socket.io.js.map'), 'utf-8');
} catch(err) {
debug('could not load sourcemap file');
}
}
return this;
};
Expected behaviour
The package should compile with webpack just fine and run smoothly.
Setup
- OS: Windows
- browser: N/A
- socket.io version: “webpack”: “^3.3.0” “socket.io”: “^2.0.3”, “socket.io-client”: “^2.0.3”
Other information (e.g. stacktraces, related issues, suggestions how to fix)
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top Results From Across the Web
How to use the webpack-dev-server/client/web_modules ...
Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues...
Read more >node.js - WebPack and Express server not working together
I'm wanting to use webpack for babel to use es6 when writing react and use its hot-module and cheap-module-source-map. But, webpack runs it's ......
Read more >Zero Config JavaScript App Prototyping with Webpack - Auth0
Then, create a folder named webpack-prototype and make it your current working directory. You can do this easily with the following command:
Read more >Development - webpack
webpack is a module bundler. Its main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable...
Read more >HMR and Hot Reloading with the webpack-dev-server
"ReferenceError: window is not defined" (if hmr is true); "TypeError: Cannot read property 'prototype' of undefined" (if inline is true). Client Side rendering ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Try disabling socket.io’s serving client files feat.
const io = require('socket.io')(httpServer, { serveClient: false })
Not ideal, but at least the bundle works.
I think this is the same error, I get on runtime by using webpack
I don’t use
socket.io
self, but it’s a dependency ofandroidjs
if I use
raw-loader
by the follow patch, it works