4

I have a JS file holding my firebase config that I import into my index.html, and it works perfectly when the values within the JS file are hardcoded

<script src="./firebase-config.js"></script>
  <script type="module">
    // Your web app's Firebase configuration
    // For Firebase JS SDK v7.20.0 and later, measurementId is optional

    import { firebaseConfig } from './firebase-config.js';
    // Initialize Firebase
    firebase.initializeApp(firebaseConfig);
    firebase.analytics();
  </script>
export var firebaseConfig = {
    apiKey: "<my-api-key>",
    authDomain: "<my-auth-domain>",
    databaseURL: "<my-database-url>",
    projectId: "<my-project-id>",
    storageBucket: "<my-storage-bucket>",
    messagingSenderId: "<my-messaging-sender-id>",
    appId: "<my-app-id>",
    measurementId: "<my-measurement-id>"
  };

But when I try to use env variables within the JS file like apiKey: process.env.FIREBASE_API_KEY, the key doesn't get picked up and I get an error that firebase isn't initialised when I run the project. The .env file is also placed in the web folder

1 Answers1

0

Usually, env var are replaced during the building process. If you dont use a bundler, you cant use env var in your app.

https://stackoverflow.com/a/61725261/3573340

Clem
  • 2,150
  • 15
  • 17