简体   繁体   中英

I can't get Firebase to work on my React project

I am trying to get firebase to work on my React project (using Webpack v3), but I am having some issues.

I added "firebase": "^4.10.1" to my package.json and in a firebase.js file I added this:

import * as firebase from 'firebase';

const config = {
    apiKey: "API_KEY",
    authDomain: "DOMAIN",
    databaseURL: "DATA_URL",
    projectId: "ID",
    storageBucket: "STORAGE_BUCKET",
    messagingSenderId: "SENDER_ID"
};

firebase.initializeApp(config);

firebase.database().ref().set({
    message: 'Connection Successful!'
});

On my app.js I imported that firebase.js file. import './firebase/firebase';

If I am not mistaking I am supposed to get the "Connection Successful!" message over in my database, but it's not working.

Instead I get a console error . I looked around online and found a few people with similar errors saying that adding:

node: {
console: true,
fs: 'empty',
child_process: 'empty',
net: 'empty',
tls: 'empty',
dgram: 'empty',
dns: 'empty',
}

to my webpack.config.json would fix it. I tried it and id didn't really work.

I didn't get those errors any more, but instead I get Uncaught TypeError: util.inherits is not a function (full error here ). I kept looking and found this on the firebase documentation where they use var firebase = require("firebase/app"); instead of import . I tried it and still didn't work.

I've tried to yarn add util , but to no avail.

Any idea why this is happening and /or how to fix it? Thanks.

I use Firebase in React this way:

import firebase from 'firebase'
var config = {
    ...
};
var fire = firebase.initializeApp(config);
fire.database().ref()...
  1. import firebase and not import * as firebase , don't know what Firebase module do export, but maybe this change the things;
  2. I init a variabile to which I assign the result of Firebase initialization ( var fire = firebase.initalize... ), then I use that to make calls to db, storage and whatever.

To me, it works flawlessly, don't know if it could be due to different versions or if it's just the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM