0

Node.js Code I have in index.js

let express = require('express')
const path = require('path');
import { initializeApp } from 'firebase/app';
import { getDatabase } from "firebase/database";
const firebaseConfig = {
    ...
};
const firebaseApp = initializeApp(firebaseConfig);
const database = getDatabase(firebaseApp);

let app = express()
const port = 8080
app.get('/updateRelay/:relayId/:status', function (req, res) {
    const relayId = req.params["relayId"]
    const status = req.params["status"]
    console.log(relayId,status)
    let updateObject = {}
    updateObject[relayId] = status
    database.ref("iot-device-001/status").set(updateObject, function(error) {
        if (error) {
            // The write failed...
            console.log("Failed with error: " + error)
        } else {
            // The write was successful...
            console.log("success")
        }
    })
});

app.get('/', function (req, res) {
    res.sendFile(path.join(__dirname, '/index.html'));
});

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`)
})

I cannot for the love of God figure out what is wrong with this code. I have tried every documentation and tutorial available and end up with some unexplainable error. Its either this or its Module Not Found. Here is the link for the tutorial I followed which gave me Module Not Found error

Here is the error I have right now

import { initializeApp } from 'firebase/app';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1001:16)
    at Module._compile (internal/modules/cjs/loader.js:1049:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47
Ebenezer Isaac
  • 772
  • 1
  • 8
  • 31
  • Did you mark your package.json with `"type": "module"`? See [Modules: CommonJS modules > Enabling](https://nodejs.org/docs/latest/api/modules.html#enabling) – Wyck May 12 '22 at 13:47
  • If i do that, im unable to require express. Is it not possible to use both express and firebase in a single js file and run it as a webserver? @Wyck – Ebenezer Isaac May 12 '22 at 13:54
  • 1
    See this answer to [SyntaxError: Cannot use import statement outside a module](https://stackoverflow.com/a/61947868/1563833) – Wyck May 12 '22 at 14:00

3 Answers3

1

It seems that your function is not defined as being a module so you can't use importinside of it. You'll have to use require

You might solve your issue by replacing

import { initializeApp } from 'firebase/app';
import { getDatabase } from "firebase/database" 

by

var initializeApp = require('firebase/app')
var getDatabase = require('firebase/database')

You will find more explanation here : https://flexiple.com/javascript-require-vs-import/

0

It is often a bit confusing how to implement it from Node JS. The easiest way is to follow this guide for the installation in NodeJS, once the SDK is installed in your local project it is necessary to initialize it.

For that, you should follow the instructions to generate a Private key that you have to add to your project to call the services from it

Even so, it is still a bit confusing but I found a very simple guide that explains in a visual and detailed way how to do it.

0

Here is a solution that worked for me, replace

import { initializeApp } from 'firebase/app';
import { getDatabase } from "firebase/database" 

with

var {initializeApp} = require('firebase/app')
var {getDatabase} = require('firebase/database')