node.jstypescriptgrpc

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:

./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. Can anyone help with what I am missing? Thanks!


Solution

  • 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:

    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.

    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. Verify by trying it out. As a bonus you could try finding out if there's a --verbose flag to make your life easier :).