protocolbuffers / protobuf-javascript

BSD 3-Clause "New" or "Revised" License
360 stars 67 forks source link

The generated commonjs `*_pb.js` files don't work in Node.js environment #8

Open yevhenii-nadtochii opened 2 years ago

yevhenii-nadtochii commented 2 years ago

This bug is a rebirth of protocolbuffers/protobuf#9152, which has already been fixed in v3.19.1. It seems the fix hasn't been delivered to the latest version.

What version of protobuf and what language are you using? Version: v3.20.1 Language: Javascript

What operating system (Linux, Windows, ...) and version? macOs Monterey v12.3.1

What runtime / compiler are you using (e.g., python version or gcc version) Node.js v16.13.0

What did you do?

  1. Run protoc --js_out=import_style=commonjs,binary:path/to/out/dir --proto_path=. *.proto
  2. Try to import and use the generated *_pb.js files.

What did you expect to see Import and use without errors (just as in 3.19.1 - 3.19.4).

What did you see instead?

ReferenceError: window is not defined

   14 | var jspb = require('google-protobuf');
   15 | var goog = jspb;
 > 16 | var global = (function() { return this || window || global || self || Function('return this')(); }).call(null);
ben-harris-5 commented 2 years ago

FYI, this was fixed in 3.19.x by PR #9156 in the old repo, but regressed to the old behavior. I've verified that this works with protoc version 3.19.4, but fails with 3.20.1.

desytech commented 2 years ago

Any progress on this issue, running into the same problems. I'am not be able to compile an old version of libprotoc on apple silicon m2 arm64. brew install --build-from-source protobuf-3.19.4 -> Error: protobuf-3.19.4 did not build

Error using *_pb.js var global = (function() { return this || window || global || self || Function('return this')(); }).call(null);

Thanks

dibenede commented 2 years ago

3.19.4 predates the split off of protobuf-javascript from the main protobuf repo. Do you still have a problem with protobuf v21+ (there was a numbering scheme change ~3.21) and the current published google-protobuf npm package/3.21 of protobuf-javascript? The main difference is you need to start specifying a --plugin flag to protoc to point it to protoc-gen-js.

desytech commented 2 years ago

libprotoc 3.21.5

cd lib/server && protoc \
        --plugin=protoc-gen-ts=./node_modules/.bin/protoc-gen-ts \
        --plugin=protoc-gen-grpc=./node_modules/.bin/grpc_tools_node_protoc_plugin \
        --js_out=import_style=commonjs,binary:./src/proto \
        --ts_out=service=grpc-node,mode=grpc-js:./src/proto \
        --grpc_out=grpc_js:./src/proto \
        -I ../../protos ../../protos/xxx.proto
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.
make: *** [gen-server] Error 1

Error mentioned in https://github.com/protocolbuffers/protobuf-javascript/issues/127 --plugin flag already in use as you recommended

libprotoc 3.20.1 var global = (function() { return this || window || global || self || Function('return this')(); }).call(null);

Any other ideas i could get the last working version of libprotoc 3.19.4 running on arm64?

dibenede commented 2 years ago

The surface level issue appears to be that it can't find protoc-gen-js on your PATH. However, I'm not familiar with either protoc-gen-ts or protoc-gen-grpc, so you may have better luck asking one of those projects how they depend on protoc-gen-js.

nottmey commented 1 year ago

We are forced to use 3.20.3 (where this bug is still present) because of https://github.com/protocolbuffers/protobuf-javascript/issues/127, but were able to workaround this via

// @ts-ignore
globalThis.window = null;
// @ts-ignore
globalThis.self = null;

at the start of our index.ts.

tbarlow12 commented 11 months ago

I'm seeing this same issue - can we re-open this issue?

dibenede commented 11 months ago

@tbarlow12 Can you please describe your setup and any reproduction instructions?