Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Cloud Functions: require(...) is not a function

I am trying to deploy a Google Cloud function, I started by just adding the initial requirements to my index.js file:

// Import the Google Cloud client libraries
const nl = require('@google-cloud/language')();
const speech = require('@google-cloud/speech')();
const storage = require('@google-cloud/storage')();

But I get the following message when deploying:

Detailed stack trace: TypeError: require(...) is not a function

This only happens with the @google-cloud/speech and @google-cloud/language modules, the @google-cloud/storage module is loaded fine as a function (I tested by commenting the first two).

Any advise will be greatly appreciated.

Borrigan

like image 597
Borrigan Avatar asked Nov 07 '17 03:11

Borrigan


People also ask

Are Google cloud Functions always running?

By essence Cloud Functions are implementing a serverless architecture: they don't run all the time (i.e. permanently), but when they are triggered.

What types of Cloud Functions exist?

There are two types of Cloud Functions: HTTP functions, which handle HTTP requests and use HTTP triggers. See Write HTTP functions for information about implementing HTTP functions. Event-driven functions, which handle events from your cloud environment and use event triggers as described in Cloud Functions triggers.

How many requests can handle a single cloud function?

By default each Cloud Run container instance can receive up to 80 requests at the same time; you can increase this to a maximum of 1000.

What is the difference between on call and on request function?

onRequest creates a standard API endpoint, and you'll use whatever methods your client-side code normally uses to make. HTTP requests to interact with them. onCall creates a callable. Once you get used to them, onCall is less effort to write, but you don't have all the flexibility you might be used to.


3 Answers

With reference to this Github comment, there was some changes in google-cloud v2 package

so you import packages such as:

const {Storage} = require('@google-cloud/storage');
const storage = new Storage({
  // config...
});
like image 186
pariola Avatar answered Oct 22 '22 16:10

pariola


Google cloud function are nodejs modules so the syntax is same as nodejs syntax.

Regarding your problem:

you have to write

const storage = require('@google-cloud/storage');

(without () at the end of each statement)

So the correct declaration will be:

// Import the Google Cloud client libraries
const nl = require('@google-cloud/language');
const speech = require('@google-cloud/speech');
const storage = require('@google-cloud/storage');

I hope this helps.

like image 5
nikhil sharma Avatar answered Oct 22 '22 15:10

nikhil sharma


It tells you that whatever you required is not a function and therefore can't be invoked with ()

if you look here : https://www.npmjs.com/package/@google-cloud/language#using-the-client-library you see a service object with multiple class returning functions is being returned, so you should set it up like this:

const nl = require('@google-cloud/language');
const language = new nl.LanguageServiceClient();
like image 2
Mischa Molhoek Avatar answered Oct 22 '22 15:10

Mischa Molhoek