Grpc-web: Getting error while run grpc-web in electron... XMLHTTPRequest is not defined

Created on 4 Feb 2019  路  8Comments  路  Source: grpc/grpc-web

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)

Most helpful comment

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');

All 8 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ivan-sysoi picture ivan-sysoi  路  5Comments

pumano picture pumano  路  6Comments

NiNJAD3vel0per picture NiNJAD3vel0per  路  3Comments

verihelp-vaibhav picture verihelp-vaibhav  路  5Comments

dv29 picture dv29  路  6Comments