0

Hello,

I am currently working on a Firebase project, and it was working fine until I converted the JavaScript code to TypeScript. Since then, I’ve encountered many problems. I resolved most of the issues in the code, but there are still a couple of challenging ones that I can’t solve.

in this two lines of code:

// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.13.0/firebase-app.js";
import {  getDatabase, ref, set,  onChildAdded , onValue } from "https://www.gstatic.com/firebasejs/10.13.0/firebase-database.js";

The first error was "exports is not defined at main.js:38:23", which I fixed with this code: <script>var exports = {};</script> in HTML file.

The second error is "main.js:40: require is not defined", and I don’t know how to fix it.

If anyone can help, it would be greatly appreciated.

I tried to install firebase locally to my project and try to download webpack.config.js but the problem remains unsolved.

2
  • It sounds like you're using Node.js specific libraries. require() is a commonjs/server-side thing. Sometimes you can use tools that convert this code to code that works in browsers, like webpack but 'downloading webpack.config.js' isn't going to magically do this for you.
    – Evert
    Commented Sep 17 at 0:42
  • But if you're just intending to write ESM code, all you migh tneed to do is configure Typescript to emit ESM instead of CommonJS. I'd probably start there!
    – Evert
    Commented Sep 17 at 0:42

0