protobuf-javascript icon indicating copy to clipboard operation
protobuf-javascript copied to clipboard

Generate js file from proto file with protoc v21.1

Open patoda opened this issue 3 years ago • 45 comments

Command used -

protoc.exe --js_out=import_style=commonjs,binary:. employees.proto

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.

patoda avatar Jun 07 '22 15:06 patoda

I encountered the same issue upgrading to version 3.21.1 from 3.20.0

wfhartford avatar Jun 10 '22 23:06 wfhartford

I'm having a same issue. Is there any solution yet?

nurhusni avatar Jun 19 '22 06:06 nurhusni

Really looking forward to this fix. Would love to help in any way if I can :)

elig-salt avatar Jun 27 '22 12:06 elig-salt

I'm having the same issue on macOS:

protoc-gen-js: program not found or is not executable
Please specify a program using absolute path or make sure the program is available in your PATH system variable
--js_out: protoc-gen-js: Plugin failed with status code 1.

I don't see protoc-gen-js in any of the binary downloads; couldn't figure out whether or not there is a separate install for this or if it should be "included" in the protoc binary as before.

I've seen that the release notes for v21.0-rc1 link to a document explaining that javascript has been moved out of the main repository to allow it to evolve independently of protoc. However, the new protobuf-javascript repository does not provide any instructions that helps me resolve this issue.

Note that the v20.1 release provides an additional js asset that I also wasn't able to make use of. Couldn't find any useful instructions.

I downgraded to v20.1 and then I don't get this problem.

PS: I'm new to installing protobuf from the official releases; I've used brew in the past, but it seems the homebrew folks are having some problems with v20.1 as well, with v3.19.4 being the most recent working version.

meling avatar Jul 03 '22 18:07 meling

I also encountered the same issue when I use the v3.21.0. which verson can I use ? I want to generate js File

Hjw52 avatar Jul 08 '22 05:07 Hjw52

I have the same on 3.21.2

ggat avatar Jul 18 '22 19:07 ggat

@haberman 21.x breaks JS and there's no known workaround for OSX / brew Can you take a look and provide some guidance, please

no release notes on removing JS

no way to downgrade

brew install [email protected]
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).

Running `brew update --auto-update`...
Warning: No available formula with the name "[email protected]". Did you mean [email protected]?
 brew install [email protected]
Error: [email protected] has been disabled because it is a versioned formula!

no releases on the js split

https://github.com/protocolbuffers/protobuf-javascript

no build instructions on the js split

https://github.com/protocolbuffers/protobuf-javascript

the latest known working js release has no build instructions either

protobuf-js-3.20.1.tar.gz

clehene avatar Jul 20 '22 16:07 clehene

workaround on OSX / brew:

brew install protobuf@3
brew link --overwrite protobuf@3

Related https://github.com/Homebrew/homebrew-core/pull/105712 https://github.com/Homebrew/homebrew-core/pull/105812

clehene avatar Jul 20 '22 18:07 clehene

Installed protobuf 21.4 with homebrew, which uses protoc 3.21.4, and also got the same protoc-gen-js: program not found or is not executable error.

The suggestion by @clehene worked like a charm

russellrc-keebo avatar Jul 27 '22 16:07 russellrc-keebo

protobuf 21.4 Win x64 Compiler:

'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.

TheYoungBeast avatar Jul 31 '22 23:07 TheYoungBeast

My findings.

Clone protobu-javascript source code

git clone https://github.com/protocolbuffers/protobuf-javascript
cd protobuf-javascript/
bazel build //generator:protoc-gen-js # It might be better to use bazelisk as a launcher for bazel.

protoc-gen-js's executable is in bazel-bin/generator/

$ ls bazel-bin/generator/protoc-gen-js
bazel-bin/generator/protoc-gen-js

Pass the path to the plugin option in protoc

protoc --plugin=/path/to/protoc-gen-js

Someday there might be a formal migration guide from the protocolbuffers team.

johejo avatar Aug 03 '22 14:08 johejo

Thanks for that, @johejo. I'm trying to figure out how in the world to get this working in a Bazel context, in which we use a third party JS gRPC proto generator (rules-proto-grpc), and that thing calls into protoc, expecting protoc to know what to do to generate JS protos. Ugh..

srabraham avatar Aug 03 '22 22:08 srabraham

We are currently working on releasing binaries for protoc-gen-js, but it should be possible to build. You can find some better examples of the plugin invocation here: https://github.com/protocolbuffers/protobuf-javascript/blob/main/docs/index.md

dibenede avatar Aug 12 '22 23:08 dibenede

A simpler workaround(other than downgrading) is to download the executable from https://github.com/protocolbuffers/protobuf-javascript/releases and add it to your PATH. Then it will work as expected

lily-es avatar Dec 21 '22 09:12 lily-es

A simpler workaround(other than downgrading) is to download the executable from https://github.com/protocolbuffers/protobuf-javascript/releases and add it to your PATH. Then it will work as expected

I can validate this is a valid workaround. After downloading the protofub-javascript release .zip (protobuf-javascript-3.21.2-linux-x86_64.zip, in my case) and copying bin/protoc-gen-js into the same directory as protoc, this command will execute successfully:

protoc --js_out=js proto/simple.proto

My system is 5.15.0-56-generic #62-Ubuntu SMP Tue Nov 22 19:54:14 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

tjohander-splunk avatar Dec 22 '22 12:12 tjohander-splunk

A simpler workaround(other than downgrading) is to download the executable from https://github.com/protocolbuffers/protobuf-javascript/releases and add it to your PATH. Then it will work as expected

I can validate this is a valid workaround. After downloading the protofub-javascript release .zip (protobuf-javascript-3.21.2-linux-x86_64.zip, in my case) and copying bin/protoc-gen-js into the same directory as protoc, this command will execute successfully:

protoc --js_out=js proto/simple.proto

My system is 5.15.0-56-generic #62-Ubuntu SMP Tue Nov 22 19:54:14 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

This does not work on my machine, it's missing some dynamically linked libraries: libwinpthread-1.dll, libstdc++-6.dll. I do Angular/Javascript development a lot of years, and know some things are complicated with dependencies. But this protoc thing really kicks it off. It's like i just want to build the sample gRPC-Web Echo Example and it immediately stops that i cannot even compile those simple proto files... i mean This javascript issue seems to be open since half a year ?!? is nobody seriously using this proto stuff with javascript ? And why there is no simple workaround, i mean why even the pre-build protoc-js has even dependencies ?!?!?!?!?! Why not build a complete selfcontained executable that is working on it's own, really....

nickwinger avatar Jan 17 '23 10:01 nickwinger

Not sure it will help @nickwinger, but you may find it easier to work with Buf's protoc plugins that generates javascript/typescript. For my project, we adopted Buf's connect-go, connect-web, and the es plugins, and it has worked great for us. It generates more readable code as well.

meling avatar Jan 17 '23 23:01 meling

@meling thank you, sounds promising, i‘ll have a look :) i got more and more into the world that with JavaScript (and nodejs) you can do everything. And google with chrome and angular where pushing limits. And now this proto thing does not like JavaScript no more ?!?…

nickwinger avatar Jan 18 '23 07:01 nickwinger

I was working trying to create protoc compilations for any version and multiple language

now, I can replicated the error in a isolated container

check https://github.com/pablodz/protoc-generator/tree/feat/error-protoc-node

run

#recommended
make loadenvs
make generate verbose=true

I hope this will help to fix errors easily

pablodz avatar Jan 28 '23 23:01 pablodz

If you're forced to downgrade and are running into issues with window in strict mode, you can use commonjs_strict: https://github.com/protocolbuffers/protobuf/issues/9152#issuecomment-1411231302

Aaron1011 avatar Jan 31 '23 23:01 Aaron1011

This worked for me (after npm install google-protobuf):

Create an executable file protoc-gen-js.sh:

#!/usr/bin/env sh
cd "$(dirname "$0")"
node ./node_modules/google-protobuf/google-protobuf.js "$@"

And then when running protoc:

protoc --plugin=protoc-gen-js=./protoc-gen-js.sh --js_out=import_style=commonjs,binary:./gen-javascript --proto_path=protobuf/src path/to/my.proto

taliastocks avatar Feb 06 '23 21:02 taliastocks

Okay never mind, that didn't work at all.

taliastocks avatar Feb 07 '23 22:02 taliastocks

v3.22.0 still does not work:

protoc: stdout: . stderr: protoc-gen-js: program not found or is not executable
  Please specify a program using absolute path or make sure the program is available in your PATH system variable
  --js_out: protoc-gen-js: Plugin failed with status code 1.

Siedlerchr avatar Feb 22 '23 09:02 Siedlerchr

My findings.

Clone protobu-javascript source code

git clone https://github.com/protocolbuffers/protobuf-javascript
cd protobuf-javascript/
bazel build //generator:protoc-gen-js # It might be better to use bazelisk as a launcher for bazel.

protoc-gen-js's executable is in bazel-bin/generator/

$ ls bazel-bin/generator/protoc-gen-js
bazel-bin/generator/protoc-gen-js

Pass the path to the plugin option in protoc

protoc --plugin=/path/to/protoc-gen-js

Someday there might be a formal migration guide from the protocolbuffers team.

This worked for us, thanks @johejo! It's definitely a headache compared to how it used to "just work" though.

tinder-dthomson avatar Feb 22 '23 19:02 tinder-dthomson

I have created a npm package to help everyone. https://www.npmjs.com/package/protoc-gen-js

You can now npm i protoc-gen-js inside a package.json directory to make the '--js_out' work in "scripts" in the package.json (i.e. if one of your package.json scripts calls 'protoc --js_out') and will allow you to specify the version of protoc-gen-js in your package.json

You can also npm i -g protoc-gen-js or npm i -g protoc-gen-js@{version} and that will make '--js_out' work everywhere at the latest or specific version specified.

Right now the only version supported is '3.21.2' but I'll try to keep it up to date as new releases are made.

yinzara avatar Mar 09 '23 23:03 yinzara

There is an official first-party solution: if you npm install grpc-tools, then you simply use grpc_tools_node_protoc (which is inside node_modules/.bin) instead of protoc. See documentation.

This way I managed to do without protoc-gen-js.

pietrodn avatar Mar 23 '23 11:03 pietrodn

@pietrodn I wouldn't consider that a solution. grpc-tools latest version is still only on protobuf version 3.19.1, which is before this issue even occurs.

tinder-dthomson avatar Mar 23 '23 15:03 tinder-dthomson

Thanks for the info, I wrongly assumed that at least the official grpc-tools would be up to date. The more I use grpc, the more disappointed I am.

pietrodn avatar Mar 23 '23 15:03 pietrodn

I also build the protoc-gen-js myself now. For future reference, the command protoc --plugin=/path/to/protoc-gen-js is not correct.

You have to use protoc --plugin=protoc-gen-js=/path/to/protoc-gen-js when protoc-gen-js is not the path.

adrianimboden avatar Apr 07 '23 21:04 adrianimboden

I also build the protoc-gen-js myself now. For future reference, the command protoc --plugin=/path/to/protoc-gen-js is not correct.

You have to use protoc --plugin=protoc-gen-js=/path/to/protoc-gen-js when protoc-gen-js is not the path.

Can you explain how you built protoc-gen-js? I'm trying to create a grpc client with Angular and javascript to use with my existing grpc server. I have .proto files but nothing is working to generate .js/.ts files.

princeje avatar Apr 25 '23 18:04 princeje