I am developing a Shopify app in which I intend to use Firebase as my storage, I successfully installed it and I see the package in package.json "firebase": "^9.5.0" but when importing I get this error in the terminal ERROR in ./conn.js ┃ Module not found: Error: Can't resolve 'firebase'
I tried the solutions in other similar questions but nothing worked, should I do something special to get Firebase working with Shopify?
Here is the code that I use the exact way in my React project and it works there
import firebase from 'firebase';
const firebaseApp = firebase.initializeApp({
apiKey: "XXX",
authDomain: "XXX",
projectId: "XXX",
storageBucket: "XXX",
messagingSenderId: "XXX",
appId: "XXX",
measurementId: "XXX"
});
const db=firebaseApp.firestore();
export default db;
Please make sure to follow the steps as mentioned in the official documentation to Add Firebase to your JavaScript project.
As mentioned in this document, you should be importing as below, for your version 9 SDK:
import { initializeApp } from 'firebase/app';
However, if you are developing an app, you need to follow the guidelines from here:
Android - Add Firebase to your Android project
iOS+ - Add Firebase to your Apple project
Related
I have a Sveltekit project where I am using firebase for authentication. I am storing the firebase.js file which contains the config parameters like apiKey and authDomain in the \src\lib folder. I am storing the apiKey value in the .env file which is present at the root of the project and using the process.env.API_KEY to load the value for the same. However when I do npm run build I get an error in the browser console - "process is not defined". I tried the below approaches but none of them seem to work -
Approach 1 -
import { initializeApp } from 'firebase/app'
import { getAuth } from 'firebase/auth'
import { config } from 'dotenv'
config()
const firebaseConfig = {
apiKey: process.env.API_KEY,
authDomain: process.env.AUTH_DOMAIN,
}
const app = initializeApp(firebaseConfig)
const auth = getAuth(app)
export default auth
Approach 2 -
Here I used the env-cmd library which I found via another Stackoverflow post. Below is the updated code
import { initializeApp } from 'firebase/app'
import { getAuth } from 'firebase/auth'
const firebaseConfig = {
apiKey: process.env['API_KEY'],
authDomain: process.env['AUTH_DOMAIN'],
}
const app = initializeApp(firebaseConfig)
const auth = getAuth(app)
export default auth
I also updated the scripts in package.json like below -
"scripts": {
"dev": "env-cmd vite dev",
"build": "env-cmd vite build",
"package": "svelte-kit package",
"preview": "vite preview",
"prepare": "svelte-kit sync"
}
Neither of the approaches seem to work and I still get the same error i.e. "process is not defined". Even some of the other repos I checked on Github had the apiKey hard coded in the config file, which obviously is a bad practice, even for hobby projects.
Any ideas on how I could incorporate the firebase apiKey via the .env file?
From the firebase docs
Unlike how API keys are typically used, API keys for Firebase services are not used to control access to backend resources; that can only be done with Firebase Security Rules (to control which users can access resources) and App Check (to control which apps can access resources).
Usually, you need to fastidiously guard API keys (for example, by using a vault service or setting the keys as environment variables); however, API keys for Firebase services are ok to include in code or checked-in config files.
Your .env file should have variables like this VITE_SECURE_API_KEY=API-KEY
and you can use them in your client-side app using import.meta.env.VITE_SECURE_API_KEY.
dotenv works on the server-side for sveltekit using node-adapter, also the firebase-admin package works only on the node environment.
I'm using Firebase admin with Node + Express to update Firestore documents from Appengine. I have some tests that I want to have using the Firestore emulator. Here's the error I'm getting:
Error in post /requests Error: {"servicePath":"localhost","port":8080,
"clientConfig":{},"fallback":true,"sslCreds":{"callCredentials":{}},
"projectId":"test-project","firebaseVersion":"9.4.1","libName":"gccl",
"libVersion":"4.7.1 fire/9.4.1","ssl":false,
"customHeaders":{"Authorization":"Bearer owner"},"scopes":[
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/datastore"
]}
You need to pass auth instance to use gRPC-fallback client in browser. Use OAuth2Client from google-auth-library.
Before each test I'm calling:
var serviceAccount = require(process.env.FIREBASE_SA_CREDENTIAL);
firebaseAdmin.initializeApp({
credential: admin.credential.cert(serviceAccount),
projectId: 'test-project'
});
And the test is using a class that simply accesses Firestore like this:
this.db = firebaseAdmin.firestore();
...
I've got the following npm dependencies:
"#google-cloud/firestore": "^4.7.1",
"firebase": "^8.0.2",
"firebase-admin": "^9.4.1"
"firebase-tools": "^8.16.2"
I'm starting the emulators and running tests with:
firebase emulators:exec 'jest --verbose=false'
I can't see what's incorrect in the config - As far as I can see, the emulator should accept all auth. The error message suggests it's using some frontend library rather than a backend library, but the dependencies all appear to be correct.
OK, solved. There was an important line missing from jest config:
module.exports = {
testEnvironment: 'node'
}
testEnvironment: 'node'. This was triggering some behaviour in the Firebase libs that made them think the environment was a browser.
I'm running a simple node app in my local machine and I need to connect to firebase realtime database.
I installed firebase via npm:
npm install firebase --save
Then I initialize the app:
var firebase = require("firebase");
var config = {
apiKey: "api-key",
authDomain: "my-app-database.firebaseapp.com",
databaseURL: "https://my-url.firebaseio.com",
storageBucket: "my-app-database.appspot.com",
};
firebase.initializeApp(config);
var myRef = firebase.database().ref("collection").on("value", (snap) => {
// do something with the data
});
Then I get the error that database is not a function. I check firebase.database and is undefined, also so are firebase.auth and firebase.storage.
I followed all the steps in the docs, but I don't see anything that could be causing this.
Goodness gracious... It was as simple as requiring the other packages in the file, like this:
// firebase
const firebase = require("firebase");
// get database, auth and storage
require("firebase/auth");
require("firebase/storage");
require("firebase/database");
Nowhere in the docs or reference says that. I thought about going back a version, perhaps 4.12.x, so I went to the npm page to see the previous versions and install one of those and try, when I found this:
https://www.npmjs.com/package/firebase#include-only-the-features-you-need
Just scroll down where they mention using npm packages or Typescript and you'll find the answer.
Shout out to the Firebase team, this information can't be just in the npm page and not in the docs, getting started guides or the github repo. Not a lot of people goes to the npm page of a package for information and I went there to check previous versions, so I kind of stumbled upon it.
I am trying to import the Firebase Admin SDK in my TypeScript (Nest.js) application.
let serviceAccount = require("../../creds.json");
console.log(serviceAccount);
const firebase = require("firebase");
firebase.initializeApp(environment.firebase);
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "projectid"
});
But when I try and build the application I get the following error.
ERROR in ./node_modules/#google-cloud/firestore/src/v1beta1/firestore_client.js
Module not found: Error: Can't resolve './firestore_client_config' in '/home/jaybell/trellis-server/trellis/node_modules/#google-cloud/firestore/src/v1beta1'
# ./node_modules/#google-cloud/firestore/src/v1beta1/firestore_client.js 28:17-53
# ./node_modules/#google-cloud/firestore/src/v1beta1/index.js
# ./node_modules/#google-cloud/firestore/src/index.js
# ./src/server/main.server.ts
ERROR in ./node_modules/google-gax/lib/operations_client.js
Module not found: Error: Can't resolve './operations_client_config' in '/home/jaybell/trellis-server/trellis/node_modules/google-gax/lib'
# ./node_modules/google-gax/lib/operations_client.js 30:17-54
# ./node_modules/google-gax/index.js
# ./node_modules/#google-cloud/firestore/src/v1beta1/index.js
# ./node_modules/#google-cloud/firestore/src/index.js
# ./src/server/main.server.ts
ERROR in ./node_modules/google-gax/index.js
Module not found: Error: Can't resolve './package' in '/home/jaybell/trellis-server/trellis/node_modules/google-gax'
# ./node_modules/google-gax/index.js 65:18-38
# ./node_modules/#google-cloud/firestore/src/v1beta1/index.js
# ./node_modules/#google-cloud/firestore/src/index.js
# ./src/server/main.server.ts
I have tried to search up any connection between the admin sdk and the google-cloud firestore package but nothing has shown up. I tried to include the firestore library and initialize it as well but this error still shows up.
I included the admin sdk exactly as in the firebase docs with
import * as admin from 'firebase-admin';
after installing with
yarn add firebase-admin
I know the firebase admin sdk can communicate with firestore but unsure of why it would be throwing this error during initialization.
It might have something to do with my project including both a front end and back end component that are compiled together, Node server that serves an angular site. Could the firebase admin sdk be being compiled with the front end possibly causing this error?
Any thoughts?
Ok I think I may have solved my own problem.
The issue was was that the firebase-admin-sdk was being included in the build process and was causing it to fail.
Fix was:
install https://github.com/liady/webpack-node-externals
and add that to my webpack.config.ts file and exclude the node_modules folder.
I then had to authenticate through gcloud, instructions here:
Could not load the default credentials? (Node.js Google Compute Engine tutorial)
And now the build and serve works.
You lost add to webpack json extension for resolve
module.exports = {
...
resolve: {
extensions: ['.ts', '.js', '.json']
}
...
};
I'm using Firebase in a React Native app and authentication with a facebook token. It works fine with RealTime Database.
But not Firestore. When I call collection.get, set or add I got this error which is not caught by my handler but just dumped in the console.
(node:15795) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: k.setTimeout is not a function
I tried also as a standalone script run as node t_firebase.js.
I installed the packages with
npm install firebase
npm install firestore
I'm using node v6.11.4 and npm 3.10.10, firestore 1.1.6, firebase 4.5.1
Here is the script:
var firebase = require("firebase");
require("firebase/firestore");
var config = {
apiKey: "...",
authDomain: "....firebaseapp.com",
databaseURL: "https://....firebaseio.com",
projectId: "...",
storageBucket: "...",
messagingSenderId: "..."
};
firebase.initializeApp(config);
testFirebase();
function testFirebase(){
console.log("testFirebase");
token="...";
var credential = firebase.auth.FacebookAuthProvider.credential(token);
firebase.auth().signInWithCredential(credential)
.then(function(val){testFirestore();})
.catch((error)=>{console.log("error:"+error);});
}
function testFirestore(){
console.log("testFirestore");
var db = firebase.firestore();
var collection = db.collection("livros");
var docref = collection.add({
autor: "AdaLovelace",
name: "blabla"
}).then(function(val){ console.log("OK"); }).catch((error)=>{console.log("error:"+error);});
}
Although you mention React Native, your error is actually from node.js so I'm not certain which environment you're asking about.
React Native - The "firebase" npm module does not currently work with React Native due to a bug. See https://github.com/firebase/firebase-js-sdk/issues/183 for more details and workarounds.
Node.JS - You can use Cloud Firestore from Node.JS using the "firebase-admin" SDK. See the node.js snippets on https://firebase.google.com/docs/firestore/quickstart for more details. Cloud Firestore is not currently supported via the "firebase" SDK, but this is planned in the future. See https://github.com/firebase/firebase-js-sdk/issues/221 for more details.