Adding Firebase JavaScript SDK's to Node-red results in error 'ERR PACKAGE PATH NOT EXPORTED'

Hi everyone,
earlier this week I created this post asking if it is possible to add JavaScript Firebase SDK's to Node-red. I got redirected to official Node-red docs and also to this example.

I got the example working as seen in the picture below:

But now when I wanna start the Node-red with Firebase SDK's I get an error:
image

This is my settings.js file:

I know for sure that the latest Firebase SDK's is installed locally in Node-red:
image

I would be helpful for any help really!
Thank you in advance.

Looking at their documentation, you require firebase/app, not firebase.
See here for their docs.

1 Like

Wow! Now it doesn't generate any errors and I can load the basic flow and dashboard!

I will try immediately setting up a Firebase code and try connecting to database! But I think it will work now! :fire:

Please I need help with something.

Firebase JavaScript SDK's are tree shakable, so I need to import modules by their name like in the example from their docs below:

How can I reproduce this behavior inside functionGlobalContext?
image

Also please - what am I getting when I call now inside a Node-red function this code:

var firebase = global.get('firebase');
var firebaseDatabase = global.get('firebaseDatabase');

Do I now need to write:
const {initializeApp} = require(firebase);

When I log the firebase object I get this:
image

Without knowing anything specific about Firebase, the object returned by require('firebase/app') has a lot of functions defined on it that you can use.

The code:

const { initializeApp } = require('firebase/app');

is one way to load just the functions you want to use.

But you don't have to do it that way. You can still require the whole firebase/app module and then call the functions it provides:

const firebaseApp = require('firebase/app');
// Call `initializeApp`:
firebaseApp.initializeApp( ... )

And the same will be true of firebase/database.

So you can do the requires in functionGlobalContext just as you show. Then in your function node, you would do:

const firebaseApp = global.get('firebase');
firebaseApp.initializeApp( ... );
1 Like

This is code snippet on how to initialize Firebase from their docs:

In order to achieve this I need to setup my settings.js file like this:

firebase: require('firebase/app'),
firebaseDatabase: require('firebase/database')

And in my function I need to call this:

const firebase = global.get('firebase');
const firebaseDatabase = global.get('firebaseDatabase');

const firebaseConfig = {
//my config
};

const app = firebase.initializeApp(firebaseConfig);
const database = firebaseDatabase.getDatabase(app);

Do I get it right or I did a mistake somewhere?

That looks like it would work - assuming it behaves as their docs suggest.

1 Like

Yes it works! I can even now write data into the database!

Thank you a lot man - you just saved me :heart:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.