Reputation: 812
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
Upvotes: 1
Views: 6072
Reputation: 1926
Javscript Solution:
Simply add type field into your package.json
{
...
"type":"module"
...
}
type "commonJS" does not support import
it looks for require(..)
.
while "module" supports import
and using require
will throw syntax error require is not defined.
After that you need to change exports and import in entry point file say index.js and js files you have created yourself.
exports.api =
to
export const api =
Typescript solution:
To use modern style Create new project with typescript and move existing js codes to it.
ref: https://firebase.google.com/docs/functions/typescript
Upvotes: 0
Reputation: 55
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')
Upvotes: 0
Reputation: 13
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.
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.
Upvotes: 0
Reputation: 69
It seems that your function is not defined as being a module so you can't use import
inside 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/
Upvotes: 2