Nodejs-pubsub: Getting error: not supported when using pubsub.publish or pubsub.create topic.

Created on 10 Apr 2019  路  13Comments  路  Source: googleapis/nodejs-pubsub

Environment details

  • OS: macOS
  • Node.js version: v10.15.3
  • npm version: 6.4.1
  • @google-cloud/pubsub version: "^0.28.1"

Steps to reproduce

  1. Used the following code to publish to an existing topic with a key that works with pubsub in another app I used:
try {
    data = 'my_Data'
    const pubsub = new PubSub({
      projectId: config.gcloudProjectID,
      keyFilename: './pubSubKey.json',
    });
    const payload = JSON.stringify({ someData: data });
    const dataBuffer = Buffer.from(payload);
    const messageId = await pubsub.topic(config.topicName).publish(dataBuffer);
  } catch (err) {
    logError('Failed to publish pubsub with error: ', err);
    throw err;
  }

I used a similar setup as above in another project and it worked fine.

  1. I then get the following error:
Error: not supported
 at GoogleProtoFilesRoot.loadSync (/Users/dev/tool/node_modules/protobufjs/src/root.js:234:15)
at Object.loadSync (/Users/dev/tool/node_modules/protobufjs/src/index-light.js:69:17)
at GrpcClient.loadProtoLegacy (/Users/dev/tool/node_modules/google-gax/build/src/grpc.js:164:54)
at GrpcClient.loadProto (/Users/dev/tool/node_modules/google-gax/build/src/grpc.js:200:42)
at new PublisherClient (/Users/dev/tool/node_modules/@google-cloud/pubsub/build/src/v1/publisher_client.js:95:15)
at PubSub.getClient_ (/Users/dev/tool/node_modules/@google-cloud/pubsub/build/src/pubsub.js:620:25)
at PubSub.request (/Users/dev/tool/node_modules/@google-cloud/pubsub/build/src/pubsub.js:638:14)
at Publisher.publish_ (/Users/dev/tool/node_modules/@google-cloud/pubsub/build/src/publisher.js:141:20)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10) +0ms
pubsub p2 triaged for GA bug web

Most helpful comment

@callmehiphop
We were able to resolve this issue. Since we did server side rendering we had some of our node intertwined with libraries that set the "window" variable and stored data in it. By the time we required the google-cloud pubsub library, the window was set and util.isNode resolves to false. We moved the require that included our pubsub code to run before any libraries that set the window object, and this resolved the issue for us a s util.isNode is true by the time it runs.

All 13 comments

Looks like a gRPC issue. Could you try reinstalling your node modules and deleting any package locks you may have?

Hey @callmehiphop
Thank you for the quick reply.
I deleted both node modules and package-lock.json then ran npm install. However, unfortunately it didn't resolve the issue, it's still throwing the same error and is unable to publish

@callmehiphop Can you provide another further steps regarding this issue. Unfortunately we are still blocked due to this error.

@Msarnaot so I took a closer look at the error in question. Looks like protobuf is trying to figure out whether or not the current environment is Node or not, I guess my follow up questions would be

  1. Are you trying to run this in a browser?
  2. If you aren't in a browser, do you have any thoughts on why this might be false?

@callmehiphop I'm running this on a server where we are doing server side rendering. So the project technically has both react and node code, however we are only using the library in the server portion.

In this

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

util.global is being set to window and not global in our project. Which doesn't have .process. However, in another project we have, which is a pure Nodejs server, it resolves util.global to global. What do you recommend in this scenario?

The behavior would be different if the code was changed to priorities global over window in the definition, like so:

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

As global is defined in our case.

@callmehiphop
We were able to resolve this issue. Since we did server side rendering we had some of our node intertwined with libraries that set the "window" variable and stored data in it. By the time we required the google-cloud pubsub library, the window was set and util.isNode resolves to false. We moved the require that included our pubsub code to run before any libraries that set the window object, and this resolved the issue for us a s util.isNode is true by the time it runs.

@Msarnaot thanks for the update, glad to hear you found a work around!

@callmehiphop
We were able to resolve this issue. Since we did server side rendering we had some of our node intertwined with libraries that set the "window" variable and stored data in it. By the time we required the google-cloud pubsub library, the window was set and util.isNode resolves to false. We moved the require that included our pubsub code to run before any libraries that set the window object, and this resolved the issue for us a s util.isNode is true by the time it runs.

Can you help me I am doing server side rendering angular universal
To remove window not found i did this

const template = fs.readFileSync(path.join( '.', 'dist/browser', 'index.html')).toString();
const win = domino.createWindow(template);
global['window'] = win;
global['document'] = win.document
global['DOMTokenList'] = win.DOMTokenList;
global['Node'] = win.Node;
global['Text'] = win.Text;
global['HTMLElement'] = win.HTMLElement;
global['navigator'] = win.navigator;

Now i am getting same error as you on running npm run serve:ssr
image

Greetings! Can you open a new issue?

Getting the same error as @hbk899

@danieldanielecki I would suggest opening an issue in the protobuf package.

@hbk899 @danieldanielecki you can fix by adding process to your fake window in the server.ts file

win.process = process;

You can find more info in this article, with also other errors I encountered with Angular SSR + Firebase

@callmehiphop
We were able to resolve this issue. Since we did server side rendering we had some of our node intertwined with libraries that set the "window" variable and stored data in it. By the time we required the google-cloud pubsub library, the window was set and util.isNode resolves to false. We moved the require that included our pubsub code to run before any libraries that set the window object, and this resolved the issue for us a s util.isNode is true by the time it runs.

maza aa gya bhai
love you

Was this page helpful?
0 / 5 - 0 ratings