1

This is related to these 2 threads: Google cloud functions - cannot read property 'getApplicationDefault' Triggering Cloud Dataflow pipeline from Cloud Function - function times out

I have created a dataflow template that will copy data from GCS to BigQuery as these two examples.

As part of the initialization process, I run

npm init
npm install --save googleapis

Here is my index.js

    var {google} = require('googleapis');

exports.goWithTheDataFlow  = (event, callback) => {


const file = event.data;
  const context = event.context;

  console.log(`Event ${context.eventId}`);
  console.log(`  Event Type: ${context.eventType}`);
  console.log(`  Bucket: ${file.bucket}`);
  console.log(`  File: ${file.name}`);
  console.log(`  Metageneration: ${file.metageneration}`);
  console.log(`  Created: ${file.timeCreated}`);
  console.log(`  Updated: ${file.updated}`);

  google.auth.getApplicationDefault(function (err, authClient, projectId) {
     if (err) {
       throw err;
     }

 console.log(projectId);

 const dataflow = google.dataflow({ version: 'v1b3', auth: authClient });
        console.log(`gs://${file.bucket}/${file.name}`);
 dataflow.projects.templates.create({
   projectId: projectId,
   resource: {
     parameters: {
       inputFile: `gs://${file.bucket}/${file.name}`

     },
     jobName: 'cloud-fn-beam-test',
     gcsPath: 'gs://goldsgymdemo/templates/MyGCStoBQDFTemplate'
   }
 }, function(err, response) {
   if (err) {
     console.error("problem running dataflow template, error was: ", err);
   }
   console.log("Dataflow template response: ", response);
   callback();
 });

   });

 callback();
};

And here is my package.json (after I have run npm init & npm install --save googleapis)

{
  "name": "sample-cloud-storage",
  "version": "0.0.1"
}

When I run this function with function: goWithTheDataFlow & Trigger:

I get an error:

Deployment failure:
Function load error: Code in file index.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'googleapis'
    at Function.Module._resolveFilename (module.js:476:15)
    at Function.Module._load (module.js:424:25)
    at Module.require (module.js:504:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/user_code/index.js:1:78)
    at Module._compile (module.js:577:32)
    at Object.Module._extensions..js (module.js:586:10)
    at Module.load (module.js:494:32)
    at tryModuleLoad (module.js:453:12)
    at Function.Module._load (module.js:445:3)

Can you please help? What am I missing? Thx

Andy Cooper
  • 79
  • 2
  • 10

3 Answers3

5

You are missing dependencies in your package.json file. Add googleapis dependency to it:

{
  "name": "sample-cloud-storage",
  "version": "0.0.1",
  "dependencies": {
    "googleapis": "^21.3.0"
  }
}
komarkovich
  • 2,223
  • 10
  • 20
  • Thanks so much @komarkovich ! That did the job! For anyone else - the Full Solution: Run: npm init npm install --save googleapis set NODE_PATH = /opt/lib/node_modules (Thats the value in my case) If you ever need to remove NPM: rm -rf node_modules Finally in Cloud Function - package.json set the Depenency for GoogleAPIs as above – Andy Cooper Jun 05 '18 at 15:55
  • Hi, I am getting another error right now - it is giving an error: TypeError: Cannot read property 'auth' of undefined I am deploying my function using Cloud Functions UI. Do I need to authenticate since I am already logged into GCP? If yes, how do I provide the GCP Service Account credentials in the Cloud Functions UI? Can I set it to a constant? Referring to: https://stackoverflow.com/questions/49348220/google-cloud-functions-cannot-read-property-getapplicationdefault – Andy Cooper Jun 06 '18 at 17:38
1

It worked for me when I changed directory into the functions folder, instead of the firebase project folder and did a package install in there

cd functions
npm install [your missing package] --save
Abraham
  • 12,140
  • 4
  • 56
  • 92
1

in my case, I accidentally use devDependecy module in a file that uploaded to Firebase.

for example, in my User model class I use a property or method from this dependency

import * as firebase from "@firebase/rules-unit-testing";

but in the package.json file, that dependcy is located in devDependecy like this

"devDependencies": {
    "@firebase/rules-unit-testing": "^1.3.14",
  },

devDependencies will not deployed to the server, thats why I have "Can't find module" error

Alexa289
  • 8,089
  • 10
  • 74
  • 178