My environment variables are not loading in next.js. No it is not a duplicate of Environment variables not working (Next.JS 9.4.4).
I have a .env.local at the root.
NEXT_PUBLIC_FIREBASE_API_KEY=<key>
NEXT_PUBLIC_FIREBASE_AUTHENTICATION_DOMAIN=<key>
NEXT_PUBLIC_FIREBASE_PROJECT_ID=<key>
NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET=<key>
NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=<key>
NEXT_PUBLIC_FIREBASE_APP_ID=<key>
NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID=<key>
I am trying to load them in a firebase.tsx file also at root
import { getApp, getApps, initializeApp } from "firebase/app";
import { getAuth, signOut } from "firebase/auth";
import { getFirestore } from "firebase/firestore";
const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_REBASE_API_KEY , //need the empty string before the env file is loaded
authDomain: process.env.NEXT_PUBLIC_REBASE_AUTHENTICATION_DOMAIN,
projectId: process.env.NEXT_PUBLIC_REBASE_PROJECT_ID,
storageBucket: process.env.NEXT_PUBLIC_REBASE_STORAGE_BUCKET,
messagingSenderId: process.env.NEXT_PUBLIC_REBASE_MESSAGING_SENDER_ID ,
appId: process.env.NEXT_PUBLIC_REBASE_APP_ID,
measurementId:process.env.NEXT_PUBLIC_REBASE_MEASUREMENT_ID
};
export const USER_EXIST = "user/exist" ;
export const USER_DUPLICATE = "user/duplicate" ;
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
// // Initialize Firebase
export const app = !getApps().length ? initializeApp(firebaseConfig) : getApp();
export const db = getFirestore(app);
export async function logOut(){
await signOut(getAuth())
.catch((err) => alert(err));
}
I don't understand why it does not load.
I do seem to follow the doc Environment variable next.js doc.
Can you help me out ?
Thank you
Related
Firebase.js:
import firebase from "firebase";
const firebaseConfig = {
apiKey: <<API_KEY>>,
authDomain: "fir-42683.firebaseapp.com",
projectId: "fir-42683",
storageBucket: "fir-42683.appspot.com",
messagingSenderId: "950284829228",
appId: "1:950284829228:web:f9e6862d17650cfca38789",
measurementId: "G-VWT1P7ES8S"
};
export const db = firebase.initializeApp(firebaseConfig)
App.js
import {db } from './Firebase'
I wrote just a simple code for read data from firebase. but now, iam stuck on this error, what will i do.
ERROR in ./src/Firebase.js 3:0-32 Module not found: Error: Package path . is not exported from package D:\BROCAMP\Week 15\olx\node_modules\firebase (see exports field in D:\BROCAMP\Week 15\olx\node_modules\firebase\package.json)
The problem is const db = firebase.initializeApp(firebaseConfig)
You missed one step, this is actually const app = firebase.initializeApp(firebaseConfig)
Consider this hook which can get your db:
useGetFirestore.ts
import { getApp } from 'firebase/app';
import { getFirestore, Firestore } from 'firebase/firestore';
const useGetFirestore = (): Firestore => {
const app = getApp();
return getFirestore(app);
};
export default useGetFirestore;
The code you are calling db is actually app and that code just needs to run at the top of your app before anything:
const app = initializeApp(firebaseConfig);
Sometimes you only need this:
initializeApp(firebaseConfig);
how can I export and import firebase database?
When I do export const db = firebase.firestore(), an error will show me this message:
ERROR in ./src/firebase.js 10:0-32
Module not found: Error: Package path . is not exported from package C:\Users\sapic\OneDrive\Plocha\Revizor\node_modules\firebase (see exports field in C:\Users\sapic\OneDrive\Plocha\Revizor\node_modules\firebase\package.json)
Did you mean './firebase'?
Requests that should resolve in the current directory need to start with './'.
Requests that start with a name are treated as module requests and resolve within module directories (node_modules, C:\Users\sapic\OneDrive\Plocha\Revizor\node_modules).
If changing the source code is not an option there is also a resolve options called 'preferRelative' which tries to resolve these kind of requests in the current directory too.
Using Firestore, React, NPM.
For example
Export
import { initializeApp } from 'firebase/app'
import { getFirestore } from 'firebase/firestore'
import { getAuth } from 'firebase/auth'
const firebaseConfig = {
apiKey: process.env.REACT_APP_FIREBASE_API_KEY,
authDomain: process.env.REACT_APP_FIREBASE_AUTH_DOMAIN,
projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.REACT_APP_FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.REACT_APP_FIREBASE_APP_ID,
}
const firebaseApp = initializeApp(firebaseConfig)
export const auth = getAuth(firebaseApp)
export const db = getFirestore(firebaseApp)
Import
import { auth, db } from '../../app/firebase'
I was trying to deploy my application that uses firebase on Vercel. But it gives `Module not found: Can't resolve '../firebase' in '/vercel/path0/components', in development this works.
My firebase.js
import firebase from "firebase/compat/app";
import "firebase/compat/auth";
import "firebase/compat/firestore";
const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_API_KEY,
authDomain: process.env.NEXT_PUBLIC_AUTH_DOMAIN,
projectId: process.env.NEXT_PUBLIC_PROJECTID,
storageBucket: process.env.NEXT_PUBLIC_STORAGE_BUCKET,
messagingSenderId: process.env.NEXT_PUBLIC_MESSAGING_SENDER_ID,
appId: process.env.NEXT_PUBLIC_APP_ID,
measurementId: process.env.NEXT_PUBLIC_MEASUREMENT_ID,
};
const app = !firebase.apps.length
? firebase.initializeApp(firebaseConfig)
: firebase.app();
const db = app.firestore();
const auth = app.auth();
const provider = new firebase.auth.GoogleAuthProvider();
export { auth, db, provider };
I am import auth, db and provider where ever I want to use them.
Here is one of the file where this error occurs.
import { auth } from "../firebase";
const Sidebar = () => {
const [user] = useAuthState(auth);
I guess the error is at import { auth } from "../firebase"; part.
Is there any way to fix this? Help would be appreciated.
I'm getting errors when I try to simply load the auth and firestore from firebase.
Error message:
TypeError: (0, _app.initializeApp) is not a function. (In '(0, _app.initializeApp)(firebaseConfig)', '(0, _app.initializeApp)' is undefined)
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
const firebaseConfig = {
apiKey: "KEY",
authDomain: "app.firebaseapp.com",
projectId: "app",
storageBucket: "app.appspot.com",
messagingSenderId: "1293821378",
appId: "5647"
};
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
const auth = app.auth();
export { db, auth }
Also when I try to import the auth from another file I try this:
import { auth } from '../firebase';
If you are using Firebase version <9.0.0 which only supports name-spaced SDK, then use the older imports only and not the modular syntax. Try refactoring the code as shown below:
import firebase from "firebase/app";
import "firebase/firestore";
import "firebase/auth"
const firebaseConfig = {};
const app = firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();
const auth = firebase.auth();
export { db, auth }
Also makes sure you are referring to Web (name-spaced) tab in the documentation.
I am installed firebase logged in as well.
I am getting a completely white screen on importing it.
This is the firebase.js file
import firebase from 'firebase'
// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyBb9O35r_N--vwBZNfpxY3vZHzi4wH1oII",
authDomain: "fir-237a1.firebaseapp.com",
projectId: " fir-237a1",
storageBucket: "fir-237a1.appspot.com",
messagingSenderId: "1055799909510",
appId: "1:1055799909510:web:c90fad7d5113500585c507"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const db = app.firestore();
const auth = firebase.auth();
const provider = new firebase.auth.GoogleAuthProvider();
export { db, auth, provider };
This is where I am importing firebase, SendMail.js file
import { db } from "./firebase.js"
import firebase from 'firebase';
function SendMail() {
const { register, handleSubmit, watch, formState: { errors } } = useForm();
const dispatch = useDispatch();
const onSubmit = (formData) => {
console.log(formData);
db.collection('emails').add({
to: formData.to,
subject: formData.subject,
message: formData.message,
timestamp: firebase.firestore.FieldValue.serverTimestamp(),
});
dispatch(closeSendMessage());
};
If the files are one by another the import here:
import { db } from "./firebase.js"
Should be like this:
import { db } from "/firebase.js"
without the ".". If you use the "." the file will be searched in the folder above and that is how it looks like. It wen to the src folder witch is probable above the one where your file is.
Also update your firebase.js file to this:
import { getFirestore } from "firebase/firestore";
import { getAuth, GoogleAuthProvider } from "firebase/auth";
// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyBb9O35r_N--vwBZNfpxY3vZHzi4wH1oII",
authDomain: "fir-237a1.firebaseapp.com",
projectId: " fir-237a1",
storageBucket: "fir-237a1.appspot.com",
messagingSenderId: "1055799909510",
appId: "1:1055799909510:web:c90fad7d5113500585c507",
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const db = getFirestore();
const auth = getAuth();
const provider = new GoogleAuthProvider();
export { db, auth, provider };
and make sure you use the latest Firebase JS SDK Version 9.x
A just saw that a lot of your code has the old SKD 8 in the repo. You can either stay with that syntax by using the compad version in the new SDK or refactor your code to use the new syntax of SDK version 9. Here is a migration guid.
You need to pick one of those two options. I would recommend to migrate to the new version.
With the new SDK version you don't even need the firebase.js file. Your code would look like this:
import { getFirestore, addDoc, collection } from "firebase/firestore";
function SendMail() {
const {
register,
handleSubmit,
watch,
formState: { errors },
} = useForm();
const dispatch = useDispatch();
const onSubmit = (formData) => {
console.log(formData);
addDoc(collection(db, "emails"), {
to: formData.to,
subject: formData.subject,
message: formData.message,
timestamp: firebase.firestore.FieldValue.serverTimestamp(),
});
};
dispatch(closeSendMessage());
}