D Holthaus
D Holthaus

Reputation: 35

cannot find service account in firebase functions admin sdk

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
        });
}

Upvotes: 2

Views: 5548

Answers (3)

Sagar
Sagar

Reputation: 710

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

Upvotes: 3

Oush
Oush

Reputation: 3338

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)

Upvotes: 5

Ronnie Smith
Ronnie Smith

Reputation: 18565

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

Upvotes: 5

Related Questions