Firebase 0.9迁移至1.0部署报错:functions.config()不可用
Hey there, let's work through this issue together. The error you're seeing is mostly tied to outdated Firebase CLI tooling, and we'll also make sure your config approach aligns with the latest best practices.
Step 1: Update Your Firebase CLI (Critical!)
The error message explicitly mentions using the latest Firebase CLI, which is likely the root cause here—you updated your function dependencies but not the CLI itself. Run this command to get the latest version:
npm install -g firebase-tools@latest
Verify the update worked by checking the version:
firebase --version
You should see a version number of 10.x or higher (as of 2024).
Step 2: Replace Deprecated functions.config() with Modern Config Methods
Since functions.config() is deprecated, here are the two recommended approaches to access your configuration:
Option 1: Use Environment Variables (Recommended)
This is the standard approach for modern Firebase Functions:
- Local Development: Create a
.envfile in your functions directory with your config values:
Make sure to addMY_APP_API_KEY=your_actual_api_key MY_APP_DB_URL=your_database_url.envto your.gitignoreto avoid committing sensitive data. - In Code: Access these values using
process.env:const apiKey = process.env.MY_APP_API_KEY; const dbUrl = process.env.MY_APP_DB_URL; - Deployment: Set these environment variables in the Google Cloud Console (under your function's "Runtime, build, connections and security settings" > "Environment variables") or use the CLI command:
Note: Config set viafirebase functions:config:set myapp.api_key="your_actual_api_key" myapp.db_url="your_database_url"firebase functions:config:setwill automatically be mapped toprocess.envin newer CLI versions.
Option 2: Use Runtime Config (For Firebase Functions v2+)
If you're using the v2 functions SDK (which you should be with the latest dependencies), you can define runtime config directly in your function definition:
import { onRequest } from "firebase-functions/v2/https"; export const myFunction = onRequest({ runtimeConfig: { apiKey: process.env.MY_APP_API_KEY, dbUrl: process.env.MY_APP_DB_URL } }, (req, res) => { // Access config via the request object const apiKey = req.runtimeConfig.apiKey; // Your function logic here res.send("Success!"); });
Step 3: Verify Dependencies & Deploy
Double-check that your package.json has the latest versions of firebase-functions and firebase-admin (you already updated these, but just to confirm):
{ "dependencies": { "firebase-admin": "^12.0.0", "firebase-functions": "^4.0.0" } }
Then deploy your functions again:
firebase deploy --only functions
That should resolve the functions.config() is not available error and get your updated app running smoothly.
内容的提问来源于stack exchange,提问作者Hector

