generating proto file for reactjs with protoc command show error: 'protoc-gen-js' is not recognized

generating proto file for reactjs with protoc command show error: 'protoc-gen-js' is not recognized

  

i want generate a proto file for reactjs this is the commands I tested:

protoc --js_out=import_style=commonjs,binary:. --grpc-web_out=import_style=commonjs,mode=grpcwebtext:. *.proto

protoc -I=. ./data.proto --js_out=import_style=commonjs,binary:. --grpc-web_out=import_style=commonjs,mode=grpcwebtext:.

and I get below error:

'protoc-gen-js' is not recognized as an internal or external command,
operable program or batch file.
--js_out: protoc-gen-js: Plugin failed with status code 1.

and I downloaded these packages in c:/ drive and added them to system variable:

protobuf-javascript-3.21.2-win64
protoc-27.1-win64
protoc-gen-grpc-web-1.5.0-windows-x86_64.exe

what is the problem? and how to fix it? also check these links too here https://github.com/protocolbuffers/protobuf-javascript/pull/208

Answer

It's confusing and the repo's protobuf-javascript README is unclear.

When you run protoc --js_out=, the --js_out refers to a separate binary (plugin, in this case called protoc-gen-js) that must either be in the path or specified using the --plugin= flag.

The plugin name (e.g. protoc-gen-js) follows a pattern in which the protoc-gen prefix is removed and _out and _opt may be added to the revised name to form the protoc flags (i.e. --js_out).

Corrollary: when you add --js_out to protoc, protoc looks (absent a --plugin= flag) in the path for a binary called protoc-gen-js to invoke.

You can download the latest release here and I recommend putting the plugin in your path.

Then try the protoc command again.

© 2024 Dagalaxy. All rights reserved.