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

Viewed 4418

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

4 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...
});

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

Follow the new syntax below:

const {Storage} = require('@google-cloud/storage');
const googleCloudStorage = new Storage({
    projectId: 'your-project-id',
    keyFilename: 'path-to-json-config-file'
});
Related