Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase: Error parsing triggers: Cannot find module 'request-promise' simple cloud function

Wanted to achieve a firebase only approach to a mobile site, so I decided to make a simple API gateway to my app so I call a cloud function endpoint instead of calling my external API and expose my api keys.

I followed the simple hello world example and was ok.

As soon as I added the request-promise module as stated in Google samples (from translate and from url Shortening example) I cannot go forward. because this arises.

Error parsing triggers: Cannot find module 'request-promise'

tested with 'request' module with same results.

My index.js is really simple

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const request = require('request-promise');

// Create and Deploy Your First Cloud Functions
// https://firebase.google.com/docs/functions/write-firebase-functions

exports.wxData = functions.https.onRequest((request, response) => {
 wwurl = "https://mycurrentendpoint.com/apicall.php?key=1234567890&lat="+request.query.lat+"&lon="+request.query.lon;   
 response.send(wwurl);
});

Right now without the const request = require('request-promise'); or const request = require('request'); it deploys ok and display the url to be called.

Really don't know what to do, I already tested creating a new project and just issue this

this is my package.json content:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase serve --only functions",
    "shell": "firebase experimental:functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "firebase-admin": "~5.4.2",
    "firebase-functions": "^0.7.1"
  },
  "private": true
}

Is that difficult to implement this in Firebase cloud functions ?

thanks for your help.

like image 537
neavilag Avatar asked Nov 12 '17 04:11

neavilag


2 Answers

If you want to use an npm module in your Cloud Function, cd to the functions directory and run the command npm install request-promise or whatever the module is named. This will add the module to your package.json file. Then, when you run firebase deploy, the module will be available to your code running in Google's cloud. If you try to use a module that's not listed in package.json, your code will fail.

like image 118
Doug Stevenson Avatar answered Sep 22 '22 15:09

Doug Stevenson


I had the same issue and fixed it installing both, request and request-promise. Go to the directory where your functions live and execute:

npm install --save request request-promise

The --save will take care of updating your package.json. Then redeploy and it should work.

like image 31
satanas Avatar answered Sep 21 '22 15:09

satanas