Error Logs:
ReferenceError: XMLHttpRequest is not defined
at goog.net.DefaultXmlHttpFactory.createInstance (webpack:///./node_modules/grpc-web/index.js?:324:126)
at Object.goog.net.XmlHttp (webpack:///./node_modules/grpc-web/index.js?:322:1501)
at goog.net.XhrIo.createXhr (webpack:///./node_modules/grpc-web/index.js?:396:250)
at goog.net.XhrIo.send (webpack:///./node_modules/grpc-web/index.js?:391:345)
at module$contents$grpc$web$GrpcWebClientBase_GrpcWebClientBase.rpcCall (webpack:///./node_modules/grpc-web/index.js?:470:441)
at proto.carbine.CommandServiceClient.install (webpack:///./src/main/protocol/carbine_grpc_web_pb.js?:103:23)
at Module.eval (webpack:///./src/main/main.ts?:61:8)
at eval (webpack:///./src/main/main.ts?:66:30)
at Module../src/main/main.ts (/home/gourav.m/go/src/carbine/client/dist/main.bundle.js:119:1)
at __webpack_require__ (/home/gourav.m/go/src/carbine/client/dist/main.bundle.js:20:30)
I was trying to create a electron app that communicates with the server using grpc (grpc-web module).. I have followed the exact sequence of steps given on https://www.npmjs.com/package/grpc-web. Somehow, it threw above error.
gRPC-Web is meant to be run from the browsers only.
Mentioned in another repo, but exposing the transport as the improbable project does would allow for this project to be run on the server. It may seem counter-intuitive, but there is a big trend in server side rendered javascript applications as well as javascript rendered frameworks such as Electron referenced from this parent issue. Happy to elaborate in a separate issue, but is a dealbreaker for us to use this project on a major public application.
Is just using the official Node gRPC library an option for server-side rendering?
What is official position about supporting SSR?
gRPC-Web is meant to be run from the browsers only.
This is unhelpful as it's nice to be able to test with Node during prototyping before going through the ordeal of setting up Browserify/Webpack.
I ended up polyfilling XMLHttpRequest using the xhr2 package (I also tried xmlhttprequest with no luck). So far it seems to be working!
Just added this to the ssr entry point (or any place that will avoid bundling it and sending to the browser):
global.XMLHttpRequest = require('xhr2');
We ended up switching back to @improbable-eng/grpc-web because it supports both Node and browser transparently without any hacks on our end.
Most helpful comment
I ended up polyfilling
XMLHttpRequestusing thexhr2package (I also triedxmlhttprequestwith no luck). So far it seems to be working!Just added this to the ssr entry point (or any place that will avoid bundling it and sending to the browser):