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

commonjs no longer works in Node.js #9152

Closed
joshkel opened this issue Oct 26, 2021 · 7 comments
Closed

commonjs no longer works in Node.js #9152

joshkel opened this issue Oct 26, 2021 · 7 comments

Comments

@joshkel
Copy link
Contributor

joshkel commented Oct 26, 2021

As a result of #8864, the generated commonjs *_pb.js files no longer work in a Node.js environment, because it references window, which is undefined in Node.js.

What version of protobuf and what language are you using?
Version: 3.19.0
Language: JavaScript

What operating system (Linux, Windows, ...) and version?
Windows 10

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

What did you do?

  1. Run protoc: 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 prior to 3.19.0)

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);

Anything else we should know about your project / environment

N/A

@MarnixBouhuis
Copy link
Contributor

I missed this case in my original PR 😅. Noticed it today when I updated google-protobuf. Fixed it in this PR: #9156

@nottmey
Copy link

nottmey commented Jan 26, 2023

We are forced to use 3.20.3 (where this bug is still present) because of protocolbuffers/protobuf-javascript#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.

@maelp
Copy link

maelp commented Jan 30, 2023

@nottmey when I do this I have errors such as Cannot read properties of null (reading 'Event'), any idea where this might come from?

@nottmey
Copy link

nottmey commented Jan 30, 2023

@maelp sorry, no idea. Our generated files don't even contain any access on Event so I suspect it's an error that was previously hidden from you. (My fix also only applies to a Node.js environment. You don't wan't to do this where any code actually needs window or self.)

@maelp
Copy link

maelp commented Jan 30, 2023

@nottmey I'm using it in a Nodejs environment, but I guess there's another package which is fiddling with globalThis and expects perhaps to find something there then?

@nottmey
Copy link

nottmey commented Jan 30, 2023

@maelp yes that could be the case

@Aaron1011
Copy link

I was also forced to downgrade, but I was able to work around this issue by using commonjs_strict instead of commonjs:

protoc --js_out=import_style=commonjs_strict:. my_proto_file.proto

The generated code doesn't access window, allowing it work in strict mode.

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

7 participants