Cannot access "child_process.spawn" in client code, with Vite 3.0 and Socket 2.5.0
See original GitHub issueDescribe the bug
Issue with xmlhttprequest-ssl
and vite 3.0
when doing vite
or vite build & vite preview
To Reproduce
Socket.IO client version: 2.5.0
Vite version: 3.0
React version: 18.2.0
Client
We set the socket connection in abstract way with react-query
. The connection part looks pretty the same as your example with some tweaks, but the issue is probably deeper inside the package xmlhttprequest-ssl
import { io } from "socket.io-client";
const _socket = io(`${import.meta.env.VITE_WSS_URL}`, {
query: {
pathname: window.location.pathname,
sw: window.innerWidth,
sh: window.innerHeight,
b: browser?.name,
token: response.token,
p: 'example',
},
transports: ['websocket'],
});
Expected behavior The socket connection should be successfully established, and we should receive events
Platform:
- Chromium 102
Additional context As an error we receive
browser-external:child_process:9 Uncaught Error: Module "child_process" has been externalized for browser compatibility. Cannot access "child_process.spawn" in client code.
which leads us to It also might be connected with new Vite’s websocket connection strategy https://vitejs.dev/blog/announcing-vite3.html#dev-improvements
Issue Analytics
- State:
- Created a year ago
- Reactions:7
- Comments:16 (2 by maintainers)
Top Results From Across the Web
uncaught error: module "child_process" has been externalized ...
Module "child_process" has been externalized for browser compatibility. Cannot access "child_process.spawn" in client code. Environment. Operating System: MacOS ...
Read more >Child process | Node.js v19.3.0 Documentation
The node:child_process module provides the ability to spawn subprocesses in a manner that is similar, but not identical, to popen(3) .
Read more >JavaScript API | Vite (main branch)
When using createServer and build in the same Node.js process, both functions rely on process.env. NODE_ENV to work properly, which also depends on...
Read more >How do I debug "Error: spawn ENOENT" on node.js?
In this way you can easily know which command actually is executed and then you can find out why nodejs cannot find the...
Read more >child process
child_process.exec() : spawns a shell and runs a command within that shell, passing the stdout and stderr to a callback function when complete ......
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 to use
@esbuild-plugins/node-modules-polyfill
vite.config.ts
as followOK, so this should fix the issue: https://github.com/socketio/engine.io-client/commit/280de368092b17648b59b7467fa49f2425edcd45
Included in
engine.io-client@3.5.3
, which should be transitively imported bysocket.io-client@2.5.0
.Can anyone confirm please?