-
Notifications
You must be signed in to change notification settings - Fork 769
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
grpc-web not working in a Vite+Typescript app #1242
Comments
I'm having the same issue. Sadly, I couldn't get it to work. I also wasn't able to get Vite+JS to work. Looks like this is some issue with how Vite loads external resources. 😞 |
@loicmagne You can use |
@Krzysztof-Dziardziel Could you please tell us how you configured protobuf-ts and grpc-web to work with vite? |
@lbensaad I think the suggestion of @Krzysztof-Dziardziel is to replace grpc-web with protobuf-ts. That's what we did at my job (and it works wonderfully - the objects are much easier to work with, they are just regular JS objects) |
This is best answer. We replace grpc-web with protobuf-ts, and every problem have been solved. |
Thanks @FredrikMeyer, I read it above, but I thought that |
@sampajano is there any plan to fix grpc-web? I just used the command from the README.md for TypeScript:
But I don't get generated the promissed |
@lbensaad @FredrikMeyer when replacing gprc-web with protobuf-ts are you using @protobuf-ts/grpcweb-transport? Did you manage do receive a stream with it? I am having
|
@barrelful Thanks for reporting the issue! FYI the documentation was wrong. There is a generated TS file but with a different name than what's documented. :) I've fixed it in #1314 above. Let me know there's more blocking issues for you. Thanks! |
@sampajano thanks, actually the rest of the documentation including the example do not work.
Should be then
But also, there is a problem since when I run it in the browser I get the error: Also the ts-example/client.ts does not match the documented code on |
@barrelful this makes this lib unusable by me, once it is fixed I will try again. |
@barrelful Thanks for pointing out the typo, will fix the documentation. :)
I'm guessing that you could be missing a @barrelful @staticdev The Typescript example works for me using the new instructions added below: https://github.com/grpc/grpc-web/tree/master/net/grpc/gateway/examples/echo/ts-example/README.md Please let me know if the above works for you and if you run into any issues. Thanks! |
@sampajano thanks for updating the docs. Still the problem is NOT in the import If you run it as Vite application you will get:
On the IDE also nothing is found:
Another confusing thing is that in the new docs you added recommends using mode=grpcwebtext, but on root README.md it has mode=grpcweb. In any case, I tried both and the errors are always the same. |
That is true @barrelful, it is not a problem of the documentation I would say. I had the same frustrating experience and decided to go back to good old REST APIs. Maybe if there is a bugfix by @sampajano or other core developers I will be able to try again and give feedback. Unfortunately I could not understand the code well enough to contribute and would require much greater amount of effort on my side. |
@barrelful @catarse-natural thanks for the feedbacks.. @barrelful I'm guessing the issue is Vite specific, is that right? I'm not really familiar with the framework, so i can't really comment on why this is happening.. If you could take a look at your setup v.s. the one i mentioned in the documentation, maybe you can identify what exactly is different (e.g. runtime, different JS bundler (we used WebPack in the example), etc.), and maybe that would give a hint on the nature of this issue.. thanks :) |
@sampajano I spent hours more testing, tried all combinations of I am not really sure if it is Vite specific or not. It is hard to isolate the error since I have already a Vite+Typescript+Vue+npm application, and the example on the repo has Typescript+Webpack+JQuery have does not use Vite or Vue. Would be possible for us to have an extended Echo example in a Typescript Vue component without Webpack? A quick way to generate a project like that with Vite is: sudo npm install -g @vue/cli
npm install -D typescript @vue/cli-plugin-typescript
npm create vue@3
# answer questions
npm install |
thanks for trying all the above mentioned options..
Sorry i'm not an expert on JS Bundling to know whether we can do without Webpack. But as far as i understand, some bundler needs to be used so that the dependencies can correctly resolve. Note that echo_pb.js is generated not by grpc-web but by protobuf-javascript so we cannot easily modify the output. My understanding is that Webpack will ensure that the dependencies are resolved correctly and generate a single JS file for the whole app. Are you not using any JS Bundler at all? (e.g. wouldn't code size be an issue for you?) I'm guessing you proved that the code does not work without one. It would be good if you can try some JS bundler and let us know if the compilation succeeds or if the issue remains afterward. thanks :) |
Are there any updates on this issue? |
I have same issue right now, now I think using vite + react ts was a bad idea for playing around with grpc in browser. |
Hello i have been facing the same issue here, I solve it and I created a starter demo example for people also strugling with it. https://github.com/Aymeric-Henry/GRPC-Vite-TS-Svelte if you have commentary about it I open to criticism |
@Aymeric-Henry thx, I used vitejs/vite#8926 < 1st solution and it's worked for me |
@Shegl Can you please provide a MWE or a link a project, where that solution works. Thx. |
@boldt hey, in the end it was not working as expected, I use protobuf-ts when I use Vite. |
I think I may have gotten this to work and so am sharing what no longer throws the errors similar to the one mentioned by OP when using gRPC-Web and Vite/TypeScript - for me after running
importing the client type from the gRPC-Web generated file can be done using an import statement: Hope this is a helpful working solution for people looking to use gRPC-Web with Vite/Typescript! |
@mvonwaldner Thanks so much for sharing your solution! Very much appreciated!! To others who had issues earlier: Does the above solution work for you? If this works for some, i'll document this 😃 Thanks! |
No it does not work
On the js file there is this import
I have used on yarn packages I have
|
@adlion Thanks for your report! Could you check if you're following the rough steps laid out in our demo app here: Note that one of the required steps there is |
I had tried compiling using the typescript or commonjs and was not working. One solution is the first solution here |
Hey, I created minimal (not) working examples: https://github.com/boldt/grpcweb-cra-to-vite They show, that the provides solutions by @martinpokorny and @mvonwaldner do not work. I would like to invite @sampajano, @martinpokorny and @mvonwaldner to help us to get that MWEs work. Probably I am just missing a small piece of the puzzle. Works with the old CRAhttps://github.com/boldt/grpcweb-cra-to-vite/tree/main It works as expected. Does not work: Solution by martinpokorny@see comment from @martinpokorny: #1242 (comment) https://github.com/boldt/grpcweb-cra-to-vite/tree/martinpokorny It throws:
Does not work: Solution by mvonwaldner@see comment from @mvonwaldner: vitejs/vite#8926 (reply in thread) https://github.com/boldt/grpcweb-cra-to-vite/tree/mvonwaldner It throws:
|
I created a working demo for this issue. Vite apps cannot import CommonJS files (like generated grpc-web files) directly, but it is possible if we make those generated CommonJS files as a local dependency and import from that dep. Details are in the repo. Too lazy to create the whole envoy proxy and grpc servers but I'm working on a project where grpc-web with vite successfully communicates with grpc servers, so I can assure you this is one way of doing this right. |
I found the missing piece of the puzzle. When I add the following to my
|
@boldt Thanks so much for creating a minimal repro case to help the debug. And very happy to hear that you've found the missing puzzle! Does that mean that, with your approach, we can resolve this issue now? 😃 |
@a2not Wow thanks for providing a working demo too! It's great to hear that these working solutions exist now! 😃 |
I personally think this is not grpc-web's fault. Whether or not grpc-web should support generation of ES Module files is for sure another topic to consider, because Vite and other tools and js community in general are leaning towards ESM rather than CJS. But we can discuss that in #535. It is |
Yeah thanks for your opinion! I agree that ES Module is how things should be done in today's world and we'll definitely consider modernizing towards that!
Do you mean this is now possible? 😃 |
@sampajano oh, sorry for the typo. It's now possible! It's not about grpc-web, but about how you use Vite properly in my opinion. |
Thanks everyone for helping figuring this out! I'm closing this issue for now 😊 |
For anyone who came here looking for a solution, please read the protobuf-ts manual: https://github.com/timostamm/protobuf-ts/blob/main/MANUAL.md it works perfectly (provided that you have enabled grpc-web in the backend). |
I used
protoc -I=. protos/*.proto --js_out=import_style=commonjs,binary:grpcproto_web/ --grpc-web_out=import_style=typescript,mode=grpcweb:grpcproto_web/
to generate my proto file.Then whenever I try to import proto objects like
import { LoginRequest } from '../protos/login_pb';
I get errorsUncaught SyntaxError: import not found: LoginRequest
.I looked at issues #535 or vite#3898 but nothing works.
Does anyone have a working example using Vite+Typescript with grpc-web ?
The text was updated successfully, but these errors were encountered: