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

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

Open
yevhenii-nadtochii opened this issue May 4, 2022 · 8 comments · Fixed by #106
Open
Labels
javascript question Further information is requested

Comments

@yevhenii-nadtochii
Copy link

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);
@yevhenii-nadtochii yevhenii-nadtochii changed the title The generated commonjs *_pb.js files don't work in a Node.js environment The generated commonjs *_pb.js files don't work in Node.js environment May 4, 2022
@devjgm devjgm transferred this issue from protocolbuffers/protobuf May 11, 2022
@ben-harris-5
Copy link

ben-harris-5 commented May 18, 2022

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
Copy link

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
Copy link
Contributor

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
Copy link

desytech commented Aug 30, 2022

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 #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
Copy link
Contributor

dibenede commented Sep 1, 2022

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
Copy link

nottmey commented Jan 26, 2023

We are forced to use 3.20.3 (where this bug is still present) because of #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
Copy link

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

@dibenede dibenede reopened this Nov 13, 2023
@dibenede
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
javascript question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants