Firebase 5.11.1及以上版本出现is not a function错误求助
Hey Stefan, let's figure out why you're hitting that TypeError: firebase_app__WEBPACK_IMPORTED_MODULE_0___default.a.functions is not a function error.
The core issue here is that the functions method isn't being attached to your firebase instance properly, even though you've imported firebase/functions. Let's walk through the fixes step by step:
1. Fix the Import Syntax (Most Likely Culprit)
In Firebase v5, the default import from firebase/app might not always play nicely with modular additional imports (like firebase/functions) in some bundling setups (including the one used in react-scripts 3.0.1). Try switching to a named import for the base Firebase app:
import * as firebase from 'firebase/app'; // Update this line import 'firebase/functions'; const config = { apiKey: "XXXXX-XXXXXXX", authDomain: "XXXXXXXXX.firebaseapp.com", databaseURL: "https://XXXXXXXXXX.firebaseio.com", projectId: "XXXXXXXXX", storageBucket: "XXXXXXX.appspot.com", messagingSenderId: "XXXXXXXXX" }; firebase.initializeApp(config); const functions = firebase.functions();
This ensures you're pulling in the full base Firebase namespace, so the functions module can attach to it correctly.
2. Reset Your Firebase Dependencies
Corrupted or mismatched dependencies can sometimes cause this kind of odd behavior. Let's do a clean reinstall:
- Delete your
node_modulesfolder andpackage-lock.json(oryarn.lockif you use Yarn) - Run
npm install(oryarn install) to refresh all packages - Restart your dev server with
react-scripts start
3. Test with the Full Firebase Import (Debug Step)
If the above doesn't work, temporarily import the entire Firebase package to rule out modular import issues (note: this will bloat your bundle size, so only use it for testing):
import firebase from 'firebase'; // Import full package instead of modular const config = { // your config here }; firebase.initializeApp(config); const functions = firebase.functions();
If this works, it confirms the problem is with the modular import setup, and the first fix should resolve it long-term.
4. Double-Check Import Order
Make sure you're importing firebase/app first, then firebase/functions, before calling firebase.initializeApp(). Your current code has the right order, but it's worth confirming you haven't accidentally reordered these lines elsewhere in your project.
内容的提问来源于stack exchange,提问作者StefanHayden

