1
votes

I am trying to initialize the firebase admin sdk in my functions folder so that my cloud function can access my database with admin read/write access, but when I try to deploy my functions the following error is returned

Error: Error parsing triggers: Cannot find module '/serviceKey.json'

I have tried running npm install in the functions directory but it hasn't helped.

Here is my package.json file

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "cors": "^2.8.4",
    "express": "^4.16.3",
    "firebase": "^5.3.1",
    "firebase-admin": "^5.13.1",
    "firebase-functions": "^2.0.2"
  },
  "private": true
}

Here is my index.js file

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

    const cors = require('cors')({origin: true});

    var firebase = require("firebase");


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

    require("firebase/auth");
    require("firebase/database");
    //require("firebase/firestore");
    //require("firebase/messaging");
    require("firebase/functions");


    var serviceAccount = require("/serviceKey.json");


    // Initialize the app with a service account, granting admin 
    privileges
    admin.initializeApp({
      credential: admin.credential.cert(serviceAccount),
      databaseURL: "https://<database name>.firebaseio.com"
    });

    const displayqr = express();
    const geturl = express();


    displayqr.get('/displayqr', (request, response) => {
         console.log("response sent");
            response.send("testio/qrdisplay.html");
        });




     exports.displayqr = functions.https.onRequest(displayqr);



     exports.geturl = functions.https.onCall((email) => {// get url 
    function
        });
}
3
/serviceKey.json refers to a file in the root of your file system. That's most likely an error/typo. May be you want ./serviceKey.json? - Hiranya Jayathilaka
If you want a definitive answer, share your project file structure, especially the location of your service account file relative to the rest of the project. Without that, we can only guess what you intend to happen here. - Doug Stevenson
Also as a Functions user, there's no reason to be using a service account JSON file. You should just use application default credentials: admin.initializeApp(); - Hiranya Jayathilaka
Thanks, why is the dot necessary, that fixed it but I am not sure why - D Holthaus

3 Answers

4
votes

You put the in the same folder as your index.js and say

var serviceAccount = require("./myfirebaseapp-firebase-adminsdk-my-secret-key.json");

and after that you admin.initializeApp

4
votes

In my case, I had the service-account file at the root of the project. It worked locally, but deploying it was an issue.

../service-account.json  (Root)

What worked for me:

Incase of a Node App, move the file from the root of your project to your functions directory since firebase will host the files at different locations

./service-account.json  (Inside functions directory)
1
votes

putting json file inside lib folder worked for me (functions\lib), i think as I am using typescript , firebase will deploy javascript functions from lib file and hence it looks for json file inside lib folder