1

I'm trying to add a serviceAccountKey to my cloud functions project (Typescript)

var serviceAccount = require("/Users/kareldebedts/myAppName/functions/serviceAccountKeyName.json");

admin.initializeApp({
  storageBucket: "appName.appspot.com",
  credential: admin.credential.cert(serviceAccount),
});

When I deploy my functions I get this error:

Deployment error.
Function failed on loading user code. Error message: Code in file lib/index.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module '/Users/kareldebedts/myAppName/functions/serviceAccountKeyName.json'

What Am I doing wrong? Because if i cmd click on the path it opens the file, so the path exists.

Karel Debedts
  • 5,226
  • 9
  • 30
  • 70

1 Answers1

3

You shouldn't use local file paths on your computer when writing Cloud Functions. It will only have available the files you deployed from your "functions" folder. So you should put the service account file in the functions folder along with your source code, and require it out of there with something like require('./serviceAccount.json').

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thanks, so i put the file in my functions folder, then I added: require('./serviceAccount.json'). (I'm using Typescript). I get this error: Error parsing triggers: Cannot find module './serviceAccountKeyName.json' Require stack: - /Users/kareldebedts/myfolder/functions/lib/index.js - /usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js – Karel Debedts Dec 21 '19 at 07:56
  • Ok I found it, you have to put the file into the functions/lib folder. thanks for the help! – Karel Debedts Dec 21 '19 at 08:25
  • 1
    Actually I would not use lib, since that's where transpiled code goes. I would put it in the functions folder and require it with "../serviceAccount.json". – Doug Stevenson Dec 21 '19 at 17:07