1

I am trying to depply functions from node.js to firebase. I am getting the error

Function failed on loading user code. Error message: Error: please examine your function logs to see the error cause:

I have tried to read the documentation but I can't wrap my head around it.

I am very new to node, (like 3 days new.) But I did read that node.js was updated to node 10 from 8. Can someone please tell me if my function logs are incorrect??

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

var admin = require("firebase-admin");

var serviceAccount = require("mydirectory");

admin.initializeApp({
 credential: admin.credential.cert(serviceAccount),
 databaseURL: "myfirebaseproject.com"
});


exports.sendPushNotifications = functions.https.onRequest((req, res) => {

  res.send('Attempting to send push notification')
  console.log('LOGGER --- Trying to send push message..');

  var uid = 'uidstring'

  var fcmToken = 'myFCMToken'

  return admin.database().ref('/users/' + uid).once('value', snapshot => {

  var user = snapshot.val();

  console.log("username is " + user.name);

  var payload = {
    notification: {
      title: 'Push Notification Title',
      body: 'Test Notification Message'
    }
  }

  admin.messaging().sendToDevice(fcmToken, payload)
   .then(function(response) {
    console.log('Succesfully sent message:', response);
    console.log(response.results[0].error);

  })
  .catch(function(error) {
    console.log('Error sending message', error);
  });

  })
})

package.json

{


"name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },

 "engines": {


  "node": "10"
  },
  "dependencies": {
    "firebase-admin": "^8.10.0",
    "firebase-functions": "^3.6.1"
  },
  "devDependencies": {
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}
zach wilcox
  • 45
  • 14
  • The error is suggesting that you should look in the functions logs in the Firebase console for more information. – Doug Stevenson Jul 03 '20 at 03:36
  • @DougStevenson thank you, I narrowed it down to Detailed stack trace: Error: Cannot find module and Did you list all required modules in the package.json dependencies? I have updated my question with my package. Could you help me understand on what my next steps should be? – zach wilcox Jul 03 '20 at 03:43

1 Answers1

0

This error is usually related to the module being installed in a wrong folder. You need to install the module in the folder where the package.json "lives", where it's usually the "functions" folder, so when deploying the function, the module can be accessed.

You will probably need to run the command npm install --save firebase in the folder where the JSON file is, so you not face anymore of this issues. Once you run this command in the right folder, you should be able to deploy your function.

In addition to that, you can find similar cases, where the solution was this one in the following links:

Let me know if the information helped you!

gso_gabriel
  • 4,199
  • 1
  • 10
  • 22
  • Hello, I have tried your method but I am still faced with Detailed stack trace: Error: Cannot find module '/Users/Mypath/file.json. Did you list all required modules in the package.json dependencies? – zach wilcox Jul 06 '20 at 18:53
  • Hi @zachwilcox could you please clarify exactly what you have tried? If you could provide details on where you installed the module now, if it was globally, in which folder, etc., because this should fix your issue, once you have installed it in the correct folder. – gso_gabriel Jul 07 '20 at 13:10