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

neavilag picture neavilag · Nov 12, 2017 · Viewed 15.5k times · Source

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.

Answer

Doug Stevenson picture Doug Stevenson · Nov 12, 2017

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.