Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting error "protoc-gen-js: program not found or is not executable" when trying to compile #105

Closed
mattwelke opened this issue May 28, 2022 · 12 comments
Assignees

Comments

@mattwelke
Copy link

mattwelke commented May 28, 2022

I'm trying to use protoc to compile my Protobuf types into JavaScript. I already set up protoc on my computer by downloading it from the GitHub releases page, moving the protoc binary onto my path, and moving the includes directory into the directory on my computer recommended in its readme file.

I have the file protos/message.proto in my project:

syntax = "proto3";

option java_package = "com.mattwelke.gaprototest.server.proto";

import "google/protobuf/wrappers.proto";

message SearchRequest {
  string cid = 1;
  string dl = 2;
  string ua = 3;
  string dt = 4;
  google.protobuf.StringValue dr = 5;
}

And I have the following script to compile it:

#!/bin/env bash

protoc -I=. --js_out=. protos/message.proto

When I run that script, I get an error:

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.

In the readme file for this project, I see that it recommends compiling a different way, with different options than just all defaults, so I also tried this as my compile script:

#!/bin/env bash

protoc -I=. --js_out=import_style=commonjs,binary:. protos/message.proto

It made no difference. I got the same error.

I do have some experience with Protobuf, using it with Java and Go. For Java, I used the official Gradle plugin where you don't need to download the protoc binary or the include files, you just configure your Gradle config files and Gradle fetches the compiler and invokes it. For Go, I remember having to install a Go specific plugin. The Go plugin is well-documented in https://github.com/golang/protobuf. That makes me think the error I'm getting trying to compile to JavaScript is telling me that there is a JavaScript equivalent of that Go plugin, which I'd need to have on my system in order to compile to JavaScript.

But I can't find any documentation this plugin. I don't see anything on NPM that I can download (nothing called "protoc-gen-js" etc), nothing on the old GitHub repo for all languages (https://github.com/protocolbuffers/protobuf), and nothing on this new JavaScript-specific Protobuf repo. I tried installing the NPM package "google-protobuf" into my project, and then running the script, but I still get the error. I also tried installing that NPM package globally, thinking that perhaps there's a system binary that would be installed that I could use, but that didn't work either. Same error.

I found issue protocolbuffers/protobuf#4840 when googling my error. It talks about JS support being added in 3.0. So it might help to note that I'm using the latest version I downloaded today:

> protoc --version
libprotoc 3.21.1

I expected that compiling to JS would work because I'm using 3+.

@n0isy
Copy link

n0isy commented May 30, 2022

please roll back version to older ( 3.15 work for me)

@mattwelke
Copy link
Author

Rolling back to 3.15 worked for me. I chose 3.15.8, the latest version I could find of the 3.15.x version.

When I compile using those two compile commands above, they both work. They have different results, which are the results I expected after reading the README in this repo. The first command produces code with goog.require and the second command produces code with require(:

image

Since it's apparently possible to compile Protobuf messages to JavaScript with only protoc, should I instead create an issue in the main Protobuf GitHub repo, where protoc is published?

@amin3141
Copy link

amin3141 commented Jun 7, 2022

This issue hit me, too. I'm wondering whether protoc-gen-js is no longer supported, in which case I should seek alternatives, or whether this was an accidental omission.

Rolling back to 3.15.8 solved the issue for me as well, but staying stuck on an older version of protoc is obviously not ideal.

@Yurzel
Copy link

Yurzel commented Jun 11, 2022

https://github.com/protocolbuffers/protobuf
By a brief look It was removed in 3.21.0 for some reason. (Look for js folder)
Meaning 3.20.1 should work. I just tested my 3.19.1 and it works.

@ccakes
Copy link

ccakes commented Jun 18, 2022

See protocolbuffers/protobuf#9874

Seems like it should have been better documented

@mattwelke
Copy link
Author

@ccakes I actually found that issue before I ran into this problem. I decided to follow the GitHub repos, leading me to this repo, where the documentation provides instructions for how to use Protobuf with JavaScript. I created this issue because when I followed those instructions, it didn't work.

@GrayYoung
Copy link

But make sure your protoc is the same version as this (or compatible)!
https://www.npmjs.com/package/google-protobuf

I use the version that is 3.20.1 works, google-protobuf and protobuf-javascript are the same version.

@dibenede
Copy link
Contributor

Duplicate of #127

@dibenede dibenede marked this as a duplicate of #127 Aug 12, 2022
@yinzara
Copy link
Contributor

yinzara commented Feb 8, 2023

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.

infynyxx added a commit to infynyxx/grpc_todo_app that referenced this issue Jul 26, 2023
@IshmaelKargbo
Copy link

Install npm install -g ts-proto and this will solve the problem protoc --plugin=./node_modules/.bin/protoc-gen-ts_proto --ts_proto_out=./ ./proto/**.proto

@sianami123
Copy link

i have same issue for reactjs
any solution ?
PS D:\codes\foprix1> protoc --js_out=import_style=commonjs,binary:. --grpc-web_out=import_style=commonjs,mode=grpcwebtext:. *.proto
'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.
PS D:\codes\foprix1> protoc --version
libprotoc 27.1

@dibenede
Copy link
Contributor

I think you need to add protoc-gen-js to your PATH or use --plugin to specify its location.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests