socket.io: Error when building for WebPack
A while ago, I had realized that there is an error when trying to use socket.io-client within a WebPack module. It turns out, that “dist/debug.js” can not be located. I did a little bit of unixy research:
Ingwie@Ingwies-Macbook-Pro.local ~/W/BIRD3 $ grep -r "dist/debug" node_modules/socket.io-client/
node_modules/socket.io-client//node_modules/engine.io-client/node_modules/debug/Makefile:all: dist/debug.js
node_modules/socket.io-client//node_modules/engine.io-client/node_modules/debug/Makefile:dist/debug.js: node_modules browser.js debug.js dist
Ingwie@Ingwies-Macbook-Pro.local ~/W/BIRD3 $ find node_modules/socket.io-client -name "dist"
[empty]
Ingwie@Ingwies-Macbook-Pro.local ~/W/BIRD3 $ find node_modules -name "debug.js" | grep dist
Conclusion: The dist folder must be a virtual folder that is used during the Browserify process… Now, how exactly do I get rid of that? It’s really heavy-lifting to require("socket.io-client/socket.io") although there already IS a system that knows commonJS.
With heavy-lifting, I mean that adding the SIO client is ~350KB… A fix would be pretty awesome.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 27 (2 by maintainers)
should work fine with
webpack+babel@rgranger This’s an example on how to use
webpackandsocket.iofor the server:package.json
webpack.config.js
server.js
A sample Webpack build for the server : https://github.com/socketio/socket.io/tree/master/examples/webpack-build-server
I came across the same problem when updating to
npm 3.3.9 webpack 1.12.13 socket.io-client 1.4.5Solving this by adding
Anyone has a better solution?
I am also not sure how to add the socket.io-client library into my client-side app to connect back to the server.
I am trying to require it like so:
…and I get this error:
TypeError: undefined is not an object (evaluating 'global.WebSocket')for an anon function.I’m mostly a back-end guy so client-side javascript is not my strong suit.
Update:
require("socket.io-client/socket.io")seems to have worked, but with a warning that suggests what was mentioned above:This seems to be a pre-built javascript file. Though this is possible, it's not recommended. Try to require the original source to get better results.@freemh you save my life bro
I was getting this error with Webpack 2.2 and socket.io-client 1.7.2. I tried pretty much everything listed here and nothing worked. Eventually I stopped the errors by installing the debug module into node_modules,
npm i debug -Sand listing ‘debug’ as an external in my webpack config,externals: ['debug'],.