Google-api-nodejs-client: Uncaught TypeError: Expected `input` to be a `Function` or `Object`, got `undefined`

Created on 28 Feb 2019  路  9Comments  路  Source: googleapis/google-api-nodejs-client

Hi,
I have got the issue while accessing the google sheet API. I tried to Google Sheets API (https://developers.google.com/sheets/api/quickstart/nodejs). And made it as npm package while tried to include the package in new project. i got an issue

Uncaught TypeError: Expected input to be a Function or Object, got undefined at module.exports (index.js?2556:45) at eval (index.js?f447:22) at Object../node_modules/gtoken/build/src/index.js (app.js:5720) at __webpack_require__ (app.js:724) at fn (app.js:101) at eval (jwtclient.js?d465:63) at Object../node_modules/google-auth-library/build/src/auth/jwtclient.js (app.js:3154) at __webpack_require__ (app.js:724) at fn (app.js:101) at Object.eval (googleauth.js?0a40:73)

Please fix the issue
Ref: https://stackoverflow.com/questions/54468809/what-is-causing-typeerror-expected-input-to-be-a-function-or-object-issue/54920596#54920596

question web

Most helpful comment

@ozydingo :wave: Node.js is a thin layer on top of the V8 JavaScript parser (which is the same JavaScript parser used by Chromium).

Depending on what APIs are used, the same source will sometimes work in Node.js and the browser; there are a few cases where this compatibility can break down:

  1. when Native modules are used in node.js (Node.js can install JavaScript that relies on C++ libraries, which aren't present in the browser).
  2. when JavaScript relies on Node.js APIs that aren't available in the browser (fs, http, etc).

I've created an issue related to us better documenting these compatibility issues :+1:

You can follow the progress here:

https://github.com/googleapis/google-cloud-node/issues/2918

All 9 comments

Greetings! Could you please share an example of the code you're using to get this error?


Side note:

Please fix the issue

This isn't really a great way to engage with us :) We're here to help, but that comes off a tad unfriendly.

The error seems to be thrown by this line in the gtoken module:

const readFile = pify(fs.readFile);

The error indicates that fs.readFile is undefined. Your stack trace mentioned Webpack, are you by any chance trying to package up this library for use on the client side? That would explain why fs.readFile isn't defined.

@erickoledadevrel I also had this issue and point out the error is thrown from pify lib, I tried to use googleaps in Chrome extension, browserify was used to package the source code, even require both fs and pify in my source, this issue will still reproduce, could you please give some suggestions on this problem? thank you very much.

This is a Node library, so it won't function in a browser environment, including Chrome Extensions. Ideally a more descriptive error message would be thrown in that case.

@erickoledadevrel thank you very much for your comments, yes I already found that googleapis was for nodejs, do you have some idea about how to let client side like Chrome Extension invoke APIs with service account(I don't want user login manually)? I knew that keep the token in the client side is not safe, BUT I want to know a org unit(G Suite) of a stand user in the Chrome Extension, for now I cannot find a suitable method beside invoking Directory APIs with service account. Thank you for your time on this problem.

Unfortunately there are no libraries I'm aware of that allow you to do service account / domain-wide delegated auth in client-side JavaScript, nor would it be secure. You'll have to stand up a server (or serverless backend) that your Chrome Extension communicates with. Consider Google Cloud Functions: https://cloud.google.com/functions/

@erickoledadevrel thanks for your explanations. When you say this is a Node library and so will not work in a browser environment, can you clarify? Specifically, why is it that I was able to import Storage from a Node REPL on my dev machine but cannot do it from a browser? (Something to do with the Node REPL sharing the gcloud cli setup perhaps?) Thanks!

Node and the Browser are two different JavaScript environment with different capabilities. This library will only work on Node (server-side), not the Browser (client-side).

@ozydingo :wave: Node.js is a thin layer on top of the V8 JavaScript parser (which is the same JavaScript parser used by Chromium).

Depending on what APIs are used, the same source will sometimes work in Node.js and the browser; there are a few cases where this compatibility can break down:

  1. when Native modules are used in node.js (Node.js can install JavaScript that relies on C++ libraries, which aren't present in the browser).
  2. when JavaScript relies on Node.js APIs that aren't available in the browser (fs, http, etc).

I've created an issue related to us better documenting these compatibility issues :+1:

You can follow the progress here:

https://github.com/googleapis/google-cloud-node/issues/2918

Was this page helpful?
0 / 5 - 0 ratings