Protobuf.js: util.isNode / Error: not supported

Created on 14 May 2019  路  9Comments  路  Source: protobufjs/protobuf.js

protobuf.js version: 6.8.8
Ubuntu: 19.04
Node: 12.2.0
Angular: 7.2.15 with server-side rendering enabled

I'm seeing "Error: not supported" popping up in an Angular 7 app on Node 12.2.0. Just upgraded Node from 10.15.3, where it did not have the error.

Screen Shot 2019-05-13 at 3 35 06 PM

After a little digging, it looks like this references _/src/root.js_:233-234:

Screen Shot 2019-05-13 at 3 50 00 PM

and _/src/util/minimal.js_:55:

Screen Shot 2019-05-13 at 3 38 05 PM

After commenting out the check in root.js, everything seems to work fine. Is there another solution?

Most helpful comment

It's also an issue if you are using jsdom with mocha.

All 9 comments

The check is here: https://github.com/protobufjs/protobuf.js/blob/master/src/util/minimal.js#L55

There might be something in your stack invalidating it, or did node change something? Workaround:

protobuf.util.isNode = true; // or your own check
...

I think it might be related to Angular's server-side rendering. The util object captures global.document but not global.process, which could make sense for SSR:

Screen Shot 2019-05-14 at 8 23 34 AM

I'm having a similar, so what I found worked (which I don't think would be a hard/serious change) is in src/util/minimal.js I reordered the util.global. It was:

util.global = typeof window !== "undefined" && window
           || typeof global !== "undefined" && global
           || typeof self   !== "undefined" && self
           || this; // eslint-disable-line no-invalid-this

And I swapped it to this:

util.global = typeof global !== "undefined" && global
           || typeof window !== "undefined" && window
           || typeof self   !== "undefined" && self
           || this; // eslint-disable-line no-invalid-this

I'm using mocha with jsdom so the window object is passing first and therefore being set as the global, not the NodeJS.Global! But if I swap the order it works fine. Could this be implemented into the regular library?

Hmm, good question. The old order assumes that a global window is super unlikely inside of a node module because there isn't an implicit global scope one could accidentally declare such a variable in, while the new depends on the assumption that no dev would ever define a global var named global in a browser context, which is unlikely.

I believe this may be a conflict with NestJS people use a lot for SSR:
https://github.com/nestjs/ng-universal/blob/master/lib/utils/domino.utils.ts

If protobuf.js is initialized first, there is no error with isNode mode detection.

That makes sense.

It's also an issue if you are using jsdom with mocha.

EDIT: wrote an article about how I fixed this with angular SSR: https://medium.com/@michele.patrassi/angular-ssr-firebase-save-days-of-debugging-by-reading-these-fixes-fcb060e248bb

OLD
same, I had this with the latest version of firebase (currently 6.4.0). I needed to downgrade to "^5.9.1" to still have SSR working.

There are some valid comments up in the thread: what's the best way to approach this?

Several questions arise from this, but...

util.isNode = Boolean(process && process.versions && process.versions.node);

Fixes the problem, considering process is a global variable since node 0.1.7 and versions since 0.2.0
I will create a PR.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RP-3 picture RP-3  路  4Comments

bennycode picture bennycode  路  3Comments

andiwonder picture andiwonder  路  3Comments

thedillonb picture thedillonb  路  4Comments

wesleytodd picture wesleytodd  路  4Comments