nestjsgrpcgrpc-node

proto generated files not being compiled into dist Nestjs


I'm implementing gRPC between 2 servers. the files are being generated perfectly, here is the generate-proto.ps1 file

# Ensure the script stops on the first error
$ErrorActionPreference = "Stop"

# Path to proto files
$protoDir = "src\server\proto"

# Path to generated files
$outDir = "$protoDir\generated"

# Ensure the output directory exists
if (!(Test-Path -Path $outDir)) {
    New-Item -Path $outDir -ItemType Directory
}

# Generate gRPC code
cmd /c "node_modules\.bin\grpc_tools_node_protoc --plugin=protoc-gen-ts=node_modules\.bin\protoc-gen-ts.cmd --ts_out=grpc_js:$outDir --grpc_out=grpc_js:$outDir --js_out=import_style=commonjs,binary:$outDir -I $protoDir $protoDir\*.proto"

I generate these files by a script in package.json -> yarn run generate:proto

Scripts: {
   // ... other default nest scripts

   "generate:proto": "powershell.exe -ExecutionPolicy Bypass -File ./generate-proto.ps1"
}

The problem is, when I run "yarn run start:dev", these generated files are not compiled into the dist folder.

this is the folder in my code

and this is the folder in the dist directory

enter image description here

as you can see, only the common_pb.ts file (which i wrote) has been compiled.


Solution

  • I found a better way to do it using ts-proto and ts-protoc-gen all you have to do is:

    1- download protobuf version from github:

    https://github.com/protocolbuffers/protobuf/releases
    

    2- extract the downloaded files and add the path of the bin folder (inside the extracted folder ) to the environement variables

    3- add the dependencies to the project

    yarn add ts-proto ts-protoc-gen
    

    4- run this command in the project root:

    protoc --plugin=protoc-gen-ts_proto=<path_to_project>/node_modules/.bin/protoc-gen-ts_proto.cmd --ts_proto_out=<path_to_output_directory> --ts_proto_opt=nestJs=true --proto_path=<path_to_proto_directory> <path_to_proto_file>
    

    this will generate a single file.ts from the file.proto, containing all the classes you need