skip to Main Content

I recently encountered this error after deploying my Nuxt app to Firebase.

Error: Failed to read credentials from file service-account.json: Error: ENOENT: no such file or directory, open 'service-account.json'
  at .readCredentialFile ( /workspace/node_modules/firebase-admin/lib/app/credential-internal.js:507 )
  at .credentialFromFile ( /workspace/node_modules/firebase-admin/lib/app/credential-internal.js:480 )
  at .getApplicationDefault ( /workspace/node_modules/firebase-admin/lib/app/credential-internal.js:401 )
  at .AppStore.initializeApp ( /workspace/node_modules/firebase-admin/lib/app/lifecycle.js:34 )
  at .initializeApp ( /workspace/node_modules/firebase-admin/lib/app/lifecycle.js:102 )
  at undefined. ( file:///workspace/node_modules/firebase-frameworks/dist/firebase-aware.js:10 )
  at .ModuleJob.run ( node:internal/modules/esm/module_job:195 )

I’m using Nuxt 3 and Vuefire to do it. I already set GOOGLE_APPLICATION_CREDENTIALS in my env and service-account.json exists in project root with correct values. What am I doing wrong?

Some more context

I created my project using Nuxt’s usual npx nuxi@latest init <project-name> command. And then I ran firebase init on that directory and point it to my project in Firebase.

After that I added VueFire and followed the config instructions provided at these pages:

Per the instructions for setting up Authentication, I downloaded my service account file and put it at the project directory as service-account.json. I reference it in my .env file, like so:

GOOGLE_APPLICATION_CREDENTIALS=service-account.json

Still, when built and deployed, I find the error I quoted above from Google Cloud Function in the logs. And the site throws 500 when one visits any page. It’s like somewhere in the build or deployment process the environment variable gets dropped.

2

Answers


  1. Chosen as BEST ANSWER

    Looks like the solution is to not provide GOOGLE_APPLICATION_CREDENTIALS environment variable. Firebase Hosting/Functions automatically sets this variable to the proper credential so you don't have to.

    The documentation does not say anything about this.


  2. There are 2 possible solutions.

    1. copy .env + service-account.json to .output/server folder after the build. You can use postbuild script in package.json to execute your copy command right after the build.

    2. set the one-line-json content of service-account.json as value of GOOGLE_APPLICATION_CREDENTIALS in .env

    see in https://vuefire.vuejs.org/nuxt/environment-variables.html#Admin-SDK

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search