EN
React - firebase import error
1 answers
0 points
I have a problem with importing firebase into React.
I was trying to import the Firebase like this but it didn't work:
xxxxxxxxxx
1
import firebase from 'firebase/app';
2
import 'firebase/auth';
3
4
const app = firebase.initializeApp({
5
apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
6
authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
7
projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
8
storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
9
messagingSenderId: process.env.REACT_APP_FIREBASE_MESSAGING_SENDER_ID,
10
appId: process.env.REACT_APP_FIREBASE_APP_ID,
11
});
12
13
export const auth = app.auth();
14
export default app;
I got the following error:
xxxxxxxxxx
1
Attempted import error: 'firebase/app' does not contain a default export (imported as 'firebase').
Then I change the imports like this:
xxxxxxxxxx
1
import { initializeApp } from 'firebase/app';
2
import 'firebase/auth';
3
4
const app = initializeApp({
5
apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
6
authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
7
projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
8
storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
9
messagingSenderId: process.env.REACT_APP_FIREBASE_MESSAGING_SENDER_ID,
10
appId: process.env.REACT_APP_FIREBASE_APP_ID,
11
});
12
13
export const auth = app.auth();
14
export default app;
and now I have this error:
xxxxxxxxxx
1
TypeError: app.auth is not a function
I've done everything from the youtube tutorial but it didn't work for me. I've installed the firebase in my project using npm install firebase
, checked directory like million times, even tried installing it locally with npm install --save firebase
or changing import
into require()
, didn't work.
What is the right way to import Firebase in React?
1 answer
0 points
There were some updates on firebase, that's why you can't import it like this.
Instead of:
xxxxxxxxxx
1
import firebase from 'firebase/app';
2
import 'firebase/auth';
Use this:
xxxxxxxxxx
1
import firebase from 'firebase/compat/app';
2
import 'firebase/compat/auth';
0 commentsShow commentsAdd comment