Webpack library for node with a socket-io.client import fail on connect: document is not defined
Note: for support questions, please use one of these channels: stackoverflow or slack
You want to:
- [x] report a bug
- [x] request a feature
Not sur. Depend if its a normal behaviour or not.
Current behaviour
When using socket.io-client in a js library build by webpack the connection fail with the error : ReferenceError: document is not defined when importing in a node process.
Steps to reproduce (if the current behaviour is a bug)
See: https://github.com/AMontagu/socketIoLib
Expected behaviour
Connection working even in node and not only browser
Setup
- OS: Ubuntu 16.04
- node: 8.8.1
- socket.io version: 2.0.4
Other information (e.g. stacktraces, related issues, suggestions how to fix)
Work around :
- Pass socket io client instance in function parameters.
- Use webpack externals config to force user to define the socket.io-client he need
Suggestion: Document is not defined error come from polling function that try to add script tag dynamically. Maybe create the polling only if typeof document !== 'undefined'
I am finding this same error. @AMontagu did you ever find a solution to this?
The full error message is:
socket error ReferenceError: document is not defined
at JSONPPolling../node_modules/engine.io-client/lib/transports/polling-jsonp.js.JSONPPolling.doPoll
at JSONPPolling../node_modules/engine.io-client/lib/transports/polling.js.Polling.poll
at JSONPPolling../node_modules/engine.io-client/lib/transports/polling.js.Polling.doOpen
at JSONPPolling../node_modules/engine.io-client/lib/transport.js.Transport.open
at Socket../node_modules/engine.io-client/lib/socket.js.Socket.open
at new Socket
at Socket
at Manager../node_modules/socket.io-client/lib/manager.js.Manager.open.Manager.connect
at new Manager
at Manager
You can use one of my two workaround cited above:
- Pass socket io client instance in function parameters.
- Use webpack externals config to force user to define the socket.io-client he need
The last choice (I found after and I keep). Is to build two different library file like library.js and library.node.js. I can share you some code to do that if you go with this options (hint: you have to chance the target webpack option from web to node and vice versa) Then in your package.json file do something like:
"main": "dist/library.node.js",
"browser": "dist/library.js",
I was afraid of that. One of the points of being able to bundle with a umd target was supposed to be that it would be compatible for cdn distro and to be required, for web bundling or nodejs. But this kind of makes it not as usable if you now need to have 2 separate distro packages depending if you want to use it for web or nodejs.
Could you please explain a bit more about the first workaround?
Pass socket io client instance in function parameters.
Instead of doing:
import io from 'socket.io-client/dist/socket.io';
connect(serverEndpoint) {
const socket = io(serverEndpoint);
}
you can do:
connect(serverEndpoint, ioInstance) {
const socket = ioInstance(serverEndpoint);
}
Where ioInstance is io imported but in the user code and not in your library letting the choice of the good version of socket io outside of your library logic.
I was indeed able to reproduce.
Here, "open" in xhr returns false when the code is bundled with webpack, but not when running the code directly.
The full error message is:
socket error ReferenceError: document is not defined at JSONPPolling../node_modules/engine.io-client/lib/transports/polling-jsonp.js.JSONPPolling.doPoll at JSONPPolling../node_modules/engine.io-client/lib/transports/polling.js.Polling.poll at JSONPPolling../node_modules/engine.io-client/lib/transports/polling.js.Polling.doOpen at JSONPPolling../node_modules/engine.io-client/lib/transport.js.Transport.open at Socket../node_modules/engine.io-client/lib/socket.js.Socket.open at new Socket at Socket at Manager../node_modules/socket.io-client/lib/manager.js.Manager.open.Manager.connect at new Manager at Manager
You can disable polling to make it work:
import io from 'socket.io-client';
const socket = io(serverEndpoint, { jsonp: false });
Hi! I think you need to set target: "node" in your webpack configuration:
module.exports = {
entry: "./index.js",
output: {
filename: "output.js",
},
mode: "production",
target: "node"
};
Reference: https://webpack.js.org/configuration/target/
I don't think this is currently in the documentation, I will add it.
For future readers, please check: https://socket.io/docs/v4/client-with-bundlers/
The main subject of this issue is to have a bundle supporting both node and web target not make it work on node. Building it for the two environment separately may be the best choice. I actually doesn't need this feature anymore. So if you think it's close let it close.