0

Can I use .env file to load my firebaseconfig? I try to use import but it not work

server.js


    require("dotenv").config();
    const firebaseConfig = {
    apiKey: process.env.APIKEY,
    authDomain: process.env.AUTHDOMAIN,
    databaseURL: process.env.DATABASEURL,
    projectId: process.env.PROJECTID,
    storageBucket: process.env.STORAGEBUCKET,
    messagingSenderId: process.env.MESSAGINSENDERID,
    appId: process.env.APPID,
    };
    console.log(firebaseConfig);

config.env

 APIKEY=key
 AUTHDOMAIN=authdomain
 DATABASEURL=url
 PROJECTID=id
 STRORAGEBUCKET=bucket
 MESSAGINGSENDERID=mid
 APPID=aid

When I console.log(firebaseConfig), all object have undefinded value

{
  apiKey: undefined,
  authDomain: undefined,
  databaseURL: undefined,
  projectId: undefined,
  storageBucket: undefined,
  messagingSenderId: undefined,
  appId: undefined
}
4uckd3v
  • 1
  • 2
  • What sort of project is this for? Both [React](https://create-react-app.dev/docs/adding-custom-environment-variables) and [Vue](https://cli.vuejs.org/guide/mode-and-env.html#environment-variables) have built-in support for `.env` files. Otherwise there's [dotenv](https://www.npmjs.com/package/dotenv) to load configuration – Phil Mar 03 '22 at 02:49
  • I use node js to do this project, so i require the dotenv to read the config file. But when i console.log(firebaseConfig), all object have undefinded value – 4uckd3v Mar 03 '22 at 02:55
  • Well where's your code using `dotenv`? The default file it looks for is `.env`, not `config.env` – Phil Mar 03 '22 at 02:56
  • I edited my question, u can see the code. – 4uckd3v Mar 03 '22 at 03:03

0 Answers0