简体   繁体   English

如何从原型文件生成_grpc_pb.d.ts以便与Node app中的gRPC一起使用?

[英]How to generate the _grpc_pb.d.ts from a proto file for use with gRPC in a Node app?

Here is my npm run protoc , the line below will run: 这是我的npm run protoc ,下面的行将运行:

./node_modules/protoc/protoc/bin/protoc --proto_path=proto --js_out=import_style=commonjs,binary:src/bin --grpc_out=src/bin --plugin=protoc-gen-grpc=node_modules/grpc-tools/bin/grpc_node_plugin --ts_out=service=true:src/bin proto/authentication_service.proto

And it generates the following files: 并生成以下文件:

authentication_service_grpc_pb.js
authentication_service_pb.d.ts
authentication_service_pb.js
authentication_service_pb_service.d.ts
authentication_service_pb_service.js

At one time I was able to get it to generate a authentication_service_grpc_pb.d.ts but with the config I saved above it does not. 一次我能够使它生成一个authentication_service_grpc_pb.d.ts,但是保存在上面的配置却不能。 Can anyone help with what I am missing? 有人可以帮我解决我所缺少的吗? Thanks! 谢谢!

Take a look at the "How to use" section of the documentation and note that generating the d.ts codes is done with a different executable: 查看文档的“如何使用”部分,请注意,生成d.ts代码是使用其他可执行文件完成的:

npm install grpc_tools_node_protoc_ts --save-dev

# generate js codes via grpc-tools
grpc_tools_node_protoc \
--js_out=import_style=commonjs,binary:./your_dest_dir \
--grpc_out=./your_dest_dir \
--plugin=protoc-gen-grpc=`which grpc_tools_node_protoc_plugin` \
-I ./proto \
./your_proto_dir/*.proto

# generate d.ts codes
protoc \
--plugin=protoc-gen-ts=./node_modules/.bin/protoc-gen-ts \
--ts_out=./your_dest_dir \
-I ./proto \
./your_proto_dir/*.proto

After writing this, that's not even the root of the problem (at least for this one particular generator). 写完这些之后,这甚至不是问题的根源(至少对于这个特定的生成器而言)。 The executable in bin/ is protoc-gen-ts . bin/的可执行文件是protoc-gen-ts

When you're trying out different stuff make sure to version-control your attempts and clean out the output directory to have a reproducible environment. 当您尝试其他内容时,请确保对您的尝试进行版本控制,并清理输出目录以提供可重现的环境。

Given all of this my best guess is that the --ts-out and --js-out flags cancel each other out and you'll have to run the generator once for each output type. 考虑到所有这些,我最好的猜测是--ts-out--js-out标志相互抵消,并且您必须为每种输出类型运行一次生成器。 Verify by trying it out. 通过尝试进行验证。 As a bonus you could try finding out if there's a --verbose flag to make your life easier :). 作为奖励,您可以尝试查找--verbose标志是否使您的生活更轻松:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM