0

i created a scheduled cloud function through my firebase CLI to run every minute and collect data from Elena sports soccer APIs endpoint but I keep getting errors in my cloud function logs, here are my logs, I am new to this so I selected the Node js(request) code sample to test if the APIS is working, am I using the wrong language code sample, I was under the impression cloud functions could be coded using node.js, please help shine a light on what I'm doing wrong

11:15:00.247 PM
scheduledFunction
Hey mac , im working.....
11:15:00.249 PM
scheduledFunction
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
11:15:00.249 PM
scheduledFunction
Error: Cannot find module 'request'
11:15:00.249 PM
scheduledFunction
at Function.Module._load (internal/modules/cjs/loader.js:667:27)
11:15:00.249 PM
scheduledFunction
Require stack:
11:15:00.249 PM
scheduledFunction
at Module.require (internal/modules/cjs/loader.js:887:19)
11:15:00.249 PM
scheduledFunction
- /workspace/index.js
11:15:00.249 PM
scheduledFunction
at require (internal/modules/cjs/helpers.js:74:18)
11:15:00.249 PM
scheduledFunction
- /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/loader.js
11:15:00.249 PM
scheduledFunction
- /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/index.js
11:15:00.249 PM
scheduledFunction
at /workspace/index.js:16:19
11:15:00.249 PM
scheduledFunction
at cloudFunction (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:130:23)
11:15:00.249 PM
scheduledFunction
at /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:199:28
11:15:00.249 PM
scheduledFunction
at processTicksAndRejections (internal/process/task_queues.js:97:5)
11:15:00.250 PM
scheduledFunction
Function execution took 8 ms, finished with status: 'error'
11:16:00.242 PM
scheduledFunction
Function execution started
11:16:00.246 PM
scheduledFunction
Hey mac , im working.....
11:16:00.247 PM
scheduledFunction
Error: Cannot find module 'request'
11:16:00.247 PM
scheduledFunction
- /workspace/index.js
11:16:00.247 PM
scheduledFunction
Require stack:
11:16:00.247 PM
scheduledFunction
- /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/loader.js
11:16:00.247 PM
scheduledFunction
- /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/index.js
11:16:00.247 PM
scheduledFunction
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
11:16:00.247 PM
scheduledFunction
at Function.Module._load (internal/modules/cjs/loader.js:667:27)
11:16:00.247 PM
scheduledFunction
at Module.require (internal/modules/cjs/loader.js:887:19)
11:16:00.247 PM
scheduledFunction
at require (internal/modules/cjs/helpers.js:74:18)
11:16:00.247 PM
scheduledFunction
at /workspace/index.js:16:19
11:16:00.247 PM
scheduledFunction
at cloudFunction (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:130:23)
11:16:00.247 PM
scheduledFunction
at /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:199:28
11:16:00.247 PM
scheduledFunction
at processTicksAndRejections (internal/process/task_queues.js:97:5)
11:16:00.248 PM
scheduledFunction
Function execution took 7 ms, finished with status: 'error'
11:17:00.450 PM
scheduledFunction
Function execution started
11:17:00.460 PM
scheduledFunction
Hey mac , im working.....
11:17:00.467 PM
scheduledFunction
Error: Cannot find module 'request'
11:17:00.467 PM
scheduledFunction
- /workspace/index.js
11:17:00.467 PM
scheduledFunction
Require stack:
11:17:00.467 PM
scheduledFunction
- /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/loader.js
11:17:00.467 PM
scheduledFunction
- /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/index.js
11:17:00.467 PM
scheduledFunction
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
11:17:00.467 PM
scheduledFunction
at Function.Module._load (internal/modules/cjs/loader.js:667:27)
11:17:00.467 PM
scheduledFunction
at Module.require (internal/modules/cjs/loader.js:887:19)
11:17:00.467 PM
scheduledFunction
at require (internal/modules/cjs/helpers.js:74:18)
11:17:00.467 PM
scheduledFunction
at /workspace/index.js:16:19
11:17:00.467 PM
scheduledFunction
at cloudFunction (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:130:23)
11:17:00.467 PM
scheduledFunction
at /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:199:28
11:17:00.467 PM
scheduledFunction
at processTicksAndRejections (internal/process/task_queues.js:97:5)
11:17:00.468 PM
scheduledFunction
Function execution took 18 ms, finished with status: 'error'
11:18:00.449 PM
scheduledFunction
Function execution started
11:18:00.545 PM
scheduledFunction
Hey mac , im working.....
11:18:00.546 PM
scheduledFunction
Error: Cannot find module 'request'
11:18:00.546 PM
scheduledFunction
Require stack:
11:18:00.547 PM
scheduledFunction
- /workspace/index.js
11:18:00.547 PM
scheduledFunction
- /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/loader.js
11:18:00.547 PM
scheduledFunction
- /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/index.js
11:18:00.547 PM
scheduledFunction
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
11:18:00.547 PM
scheduledFunction
at Function.Module._load (internal/modules/cjs/loader.js:667:27)
11:18:00.547 PM
scheduledFunction
at Module.require (internal/modules/cjs/loader.js:887:19)
11:18:00.547 PM
scheduledFunction
at require (internal/modules/cjs/helpers.js:74:18)
11:18:00.547 PM
scheduledFunction
at /workspace/index.js:16:19
11:18:00.547 PM
scheduledFunction
at cloudFunction (/workspace/node_modules/firebase-functions/lib/cloud-functions.js:130:23)
11:18:00.547 PM
scheduledFunction
at /layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:199:28
11:18:00.547 PM
scheduledFunction
at processTicksAndRejections (internal/process/task_queues.js:97:5)
11:18:00.548 PM
scheduledFunction
Function execution took 100 ms, finished with status: 'error'

and here my code

const functions = require('firebase-functions');

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
//   functions.logger.info("Hello logs!", {structuredData: true});
//   response.send("Hello from Firebase!");
// });




exports.scheduledFunction = functions.pubsub.schedule('* * * * *').onRun((context) => {
  console.log('Hey mac , im working.....');
  const request = require('request');

  const options = {
    method: 'GET',
    url: 'https://elenasport-io1.p.rapidapi.com/v2/upcoming',
    qs: {page: '1'},
    headers: {
      'x-rapidapi-key': 'API KEY COMES HERE',
      'x-rapidapi-host': 'elenasport-io1.p.rapidapi.com',
      useQueryString: true
    }
  };
  
  request(options, function (error, response, body) {
      if (error) throw new Error(error);
  
      console.log(body);
  });
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
mac
  • 65
  • 7
  • Please only tag your question with things that are relevant to the problem you're asking about. While you may be using Flutter/Dart, this problem is only related to Node.js as far as I can see. Providing the correct tags help you find existing answers, such as those I linked - which came from this search: https://stackoverflow.com/search?q=%5Bnode.js%5D+Cannot+find+module+%27request%27 – Frank van Puffelen Apr 10 '21 at 23:07

1 Answers1

1

You are facing this issue because request package isn't installed in firebase cloud function. Just run

npm install request --save

from within your firebase cloud function project. It should work.

tanweer anwar
  • 117
  • 2
  • 7