When I use imports enabling type:module in the package json file I can use without problems the imports such as import { ref, set,child ,getDatabase,onValue,get,push, update} from "firebase/database"; and access the data, but I want to interact with that information with inputs or text areas but I cannot access those it throws document is not defined.!
I have read many pages but as I am new I cannot get it. I cannot apply the answer to my project.
import { ref, set,child ,getDatabase,onValue,get,push, update} from "firebase/database";
import { getAuth, onAuthStateChanged } from "firebase/auth";
//to get info from firebase
var headbox = document.getElementById('a'); // from Html to manage info send and show it here
var bodybox = document.getElementById('b'); // error when I want to read this
Although if I take those imports out and erase type:module in the package json file, I can use document.getById but I cannot use imports please help me! I am new
2
Answers
The Firebase Web SDK ideally requires module bundlers (Webpack/Rollup) or JavaScript framework tooling like Vue, React, Angular, etc because the Web SDK (or at least version 9) is optimized to work with module bundlers to eliminate unused code (tree-shaking) and decrease SDK size.
Sounds like you would be better off using CDN (content delivery network).
JavaScript
HTML
type: module
inpackage.json
is for use in Nodejs environments, hence the error. Backend has nodocument
or evenwindow
object.What you are doing is frontend, so setting up a frontend environment using
vite
/webpack
(preferablyvite
), would do you some justice.vite
is really easy to setup for what you are doing. Vite Getting Started Guide.You just place your
index.html
at the root of your working folder and run it’s commands. It loads very quickly.