temp_
temp_

Reputation: 1317

Running Cloud Functions locally gives error "functions.config() is not available"

Cloud Functions for Firebase was just recently released and I am following the instructions on a fresh install. Here is the "Get Started" page.

I have installed "npm install -g firebase-tools" and all my files are in my project. I am using WebStorm 2016.3 and Node.JS v6.10.0.

I have the firebase login and firebase init functions installed and set up as well. My set up. Here is my set up.

My package.json

 {
   "name": "functions",
   "description": "Cloud Functions for Firebase",
    "dependencies": {
     "firebase-admin": "^4.1.2",
     "firebase-functions": "^0.5"
   },
    "private": true
 }

These first two lines of code work.

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

But then when I try to run this line...

  admin.initializeApp(functions.config().firebase);

I get this error.

 Error: functions.config() is not available. Please use the latest version of the Firebase CLI to deploy this function.
   at init (/Users/.../functions/node_modules/firebase-functions/lib/config.js:46:19)
   at Object.config (/Users/.../functions/node_modules/firebase-functions/lib/config.js:29:9)
   at Object.<anonymous> (/Users/.../functions/index.js:11:31)
   at Module._compile (module.js:570:32)
   at Object.Module._extensions..js (module.js:579:10)
   at Module.load (module.js:487:32)
   at tryModuleLoad (module.js:446:12)
   at Function.Module._load (module.js:438:3)
   at Module.runMain (module.js:604:10)
   at run (bootstrap_node.js:394:7)

What am I missing here?

Upvotes: 52

Views: 23850

Answers (5)

pralay mandal
pralay mandal

Reputation: 1

As of today 19/05/2024 if you are getting this error after deployment to the cloud use admin.initializeApp(); instead of admin.initializeApp(functions.config().firebase); it solved my error.

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

admin.initializeApp();
let db = admin.firestore();

Thanks,

Upvotes: 0

Avi Vaulin
Avi Vaulin

Reputation: 89

In my case .runtimeconfig.json / .env / .env.local didn't worked - in any folder.

It turns out that there's some issue with firebase-tools related to PWD on windows.

Before you execute “firebase emulators:start”, run this one:

$env:CLOUD_RUNTIME_CONFIG="$(pwd)/functions/.runtimeconfig.json"

Upvotes: 1

Mitchell Gant
Mitchell Gant

Reputation: 974

If, like me, you got this error while trying to run your functions locally then it's because functions.config() is only available within the Cloud Functions runtime.

If you are trying to test your functions before you deploy, here is the link to the documentation on how to do so: run functions locally. Specifically, this part is of interest:

If you're using custom functions configuration variables, run the following command in the functions directory of your project before running firebase serve.

firebase functions:config:get > .runtimeconfig.json

However, if you're using Windows PowerShell, replace the above command with:

firebase functions:config:get | ac .runtimeconfig.json

Upvotes: 95

Doug
Doug

Reputation: 834

I was running firebase deploy from the wrong dir. Hopefully this will save someone some time.

Upvotes: 13

Robert-Jan Huijsman
Robert-Jan Huijsman

Reputation: 1934

That does look like you might still have an old version of the firebase CLI, even though you tried to install the newest firebase-tools.

You can check by running firebase --version. That should say at least 3.5.0. If it doesn't, you'll want to run npm install -g firebase-tools again, which should hopefully fix things.

If your firebase --version continues to show the wrong version, you'll want to check if you accidentally have multiple versions of firebase-tools installed.

Upvotes: 6

Related Questions