Issue
it's my firstime using Firebase and I think that I've followed all step to connect my project to firebase. I tried to run my project with a small js console.log(firebase)
When I went to my console I received this error message Failed to resolve module specifier "firebase/app"
I want to mention that I've create a node_module folder and I did npm i firebase.
html file:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>TP1</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.1/css/bulma.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script type="module" src="/index.js"> </script>
</head>
<body>
index.js:
import { initializeApp } from "firebase/app";
//import { getDatabase } from "firebase/database";
const firebaseConfig = {
//(my config...)
};
const app = initializeApp(firebaseConfig);
//const database = getDatabase(firebaseConfig);
console.log(firebase)
Solution
When you use import { ... } from "some/package"
, you are expected to use a bundler like Webpack or Rollup to compile your code prior to accessing it on a website. This pulls the pieces you need out from your dependencies ready for use by your code.
Especially in v9+ of the SDK, the Firebase SDK was rewritten to support these bundlers, so expect many code examples you see to have been written for the now deprecated v8 or older. How to move this older code is covered in this migration guide.
A key takeaway from that guide is that firebase
is no longer a global object. As you are new to Firebase, I would avoid writing any code that uses the old syntax. You can spot older syntax by looking for code like firebase.initializeApp()
, firebase.database()
, firebase.firestore()
and so on. In the SDK those examples have been replaced by initializeApp()
, getDatabase()
and getFirestore()
respectively.
If you intend to use modular code directly in your browser (using <script type="module" src="..."></script>
), it is important to remember that the code is isolated from the browser console - you can't run console.log(firebaseConfig)
and expect it to have your object.
This next section is not for production code, just for trying things out in a familiar environment.
If you want to tinker around with the Firebase libraries in your browser console, you will add something similar to this:
<script type="module">
window.FIREBASE_MODULES = window.FM = {};
async function loadFirebaseModule(serviceName, sinkErrors) {
const name = serviceName.toLowerCase();
if (window.FIREBASE_MODULES[name]) return window.FIREBASE_MODULES[name];
try {
// uses unpkg to get the latest semver
if (!loadFirebaseModule.version) {
const response = await fetch("https://unpkg.com/firebase/firebase-app.js", { method: "HEAD" });
const match = /@\d+(?:\.\d+)*/.exec(response.url);
if (!match) {
console.log("Unexpected resource URL (SemVer could not be determined, falling back to v9.0.0): " + response.url);
loadFirebaseModule.version = "9.0.0";
} else {
loadFirebaseModule.version = match[0].slice(1);
}
}
// use the found semver to pull from Google's CDN
const module = await import(`https://www.gstatic.com/firebasejs/${loadFirebaseModule.version}/firebase-${name}.js`);
window.FIREBASE_MODULES[name] = module;
console.log(`Successfully imported "${name}" module`)
return module;
} catch (err) {
if (sinkErrors) {
console.error(`Failed to import "${name}" module`, err);
} else {
throw err;
}
}
}
window.loadFirebaseModule = loadFirebaseModule;
</script>
That little snippet will now allow you to run code like this either in-page or in the browser's console. It returns a Promise, so make sure to wait for it to finish resolving if you are using it inside a script.
loadFirebaseModule('app')
.then(({ initializeApp }) => {
initializeApp({ /* ... config ... */ });
});
loadFirebaseModule('storage');
loadFirebaseModule('storage', true); // import & ignore the error
Once a module is loaded, you can use const { getApp } = FM.app
similar to how you would use import { getApp } from "firebase/app"
.
Answered By - samthecodingman
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.