50

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?

Grimthorr
  • 6,856
  • 5
  • 41
  • 53
temp_
  • 1,238
  • 2
  • 12
  • 24
  • 1
    2 years later, this is still a problem with no easy solution. Nothing below worked for me, nor did any other threads. – Ryan Aug 05 '19 at 10:08
  • @Ryan are you trying to run Firebase Cloud Functions? – temp_ Aug 05 '19 at 20:41
  • Yeah. Firebase functions – Ryan Aug 06 '19 at 23:31
  • @Ryan with FCF essentially you are just writing the code that will run on Firebase servers not your local machine. All console logs are in the Firebase console. Not sure if that was what you are looking for. – temp_ Aug 07 '19 at 14:50
  • I understand that. I am saying that there is still an issue with emulating the functions locally - as it has never worked for me across several projects and machines. It requires a deployment to test anything. – Ryan Aug 09 '19 at 00:59
  • @Ryan Michael's answer below worked for me. I use Typescript so I had to import the firebase, firebase-functions and firebase-admin libraries using the Typescript instructions found [here](https://firebase.google.com/docs/functions/config-env). I also had to place the .runtimeconfig.json file in the root directory of my project. – Craig Myles Dec 06 '19 at 04:11

4 Answers4

94

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
Community
  • 1
  • 1
Mitchell Gant
  • 964
  • 6
  • 3
  • 16
    For me, it was using other environment variables. I found the answer on the adjacent page which says to create a local runtime config that copies the actual config file since the config only works with the deployed code. For local, run this `firebase functions:config:get > .runtimeconfig.json`. – Martavis P. Aug 31 '17 at 07:21
  • This helped me with figuring out how to test locally before deploying: https://firebase.google.com/docs/functions/local-emulator – cking24343 Sep 05 '17 at 11:27
  • 18
    @MartavisP. you saved my day! Important note: .runtimeconfig.json should be placed inside "functions" folder, had issue because of that as well because placed to root initially. – Vladimir Tolstikov Oct 22 '17 at 10:10
  • A year later I am back to the same place. Thanks again. – MadMac Jan 10 '21 at 19:05
  • @VladimirTolstikov thanks for the note. That fixed it for me! – PhillipJacobs Jun 09 '23 at 16:52
10

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

Doug
  • 794
  • 7
  • 12
6

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.

Robert-Jan Huijsman
  • 1,914
  • 12
  • 9
  • Yes, I have 3.5.0. I will look around and see if I can uninstall Firebase and reinstall it again. Also, my firebase.json file is blank, is this right? – temp_ Mar 16 '17 at 19:21
  • I wouldn't expect that file to be blank. Even when there's no values in `firebase.json` it would contain `{}`. You might want to try running `firebase init` again. – Robert-Jan Huijsman Mar 16 '17 at 22:00
  • For me it didn't work. i needed to manually write config.json file. Check how here: https://github.com/GoogleCloudPlatform/cloud-functions-emulator/issues/74 – MiguelSlv Mar 25 '17 at 22:41
  • but `firebase` and `firebase-tools` on npm are two separate things. For example `firebase-tools` latest version is 3.17.4 - what about that? – jean d'arme Feb 28 '18 at 08:11
  • Just used `npm list -g` to see what global packages I have and sorted out that latest `firebase-tools`'s `firebase` package is version 2.4.2... – jean d'arme Feb 28 '18 at 08:30
  • The command-line tool `firebase` that you use to run `firebase deploy` is provided by the npm package `firebase-tools`. The npm package `firebase` is the Firebase JavaScript SDK. Their version numbers are not related. Hope that helps? – Robert-Jan Huijsman Mar 01 '18 at 19:13
1

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"
Avi Vaulin
  • 49
  • 1
  • 9
  • 2
    This is so strange. This wasn't a problem with `firebase-tools@9.16.0`, but as soon as I upgraded to `firebase-tools@10.0.1`, `.runtimeconfig.json` wouldn't load automatically unless I explicitly set `CLOUD_RUNTIME_CONFIG` using your command. – Johnny Oshika Jan 04 '22 at 03:02
  • 1
    I reported this issue here: https://github.com/firebase/firebase-tools/issues/3983 – Johnny Oshika Jan 04 '22 at 03:11