Utilizing environment variables in Flutter's web directory for JavaScript files: Best practices

I am currently facing an issue with my firebase configuration in a JS file that I import into my index.html. Everything works perfectly when the values in 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>"
  };

However, when attempting to use environmental variables within the JS file such as

apiKey: process.env.FIREBASE_API_KEY
, the key does not get recognized. This results in an error stating that firebase is not initialized when running the project. It should be noted that the .env file is also located in the web folder.

Answer №1

Typically, environment variables are substituted when the software is compiled. Without a bundler, utilizing env vars in your application may not be feasible.

Check out this Stack Overflow thread for more information.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What steps should be taken to validate a condition prior to launching a NextJS application?

In my NextJS project (version 13.2.4), I usually start the app by running: npm run dev But there's a new requirement where I need to check for a specific condition before starting the app. If this condition is not met, the app should exit. The condi ...

The setCountry function fails to properly change the country value

My goal is to establish a default country selection in checkbox options, I have three choices: United States, United Kingdom, and Rest of the world; Here's the constant called AVAILABLE_COUNTRIES which contains the iso codes for the mentioned countrie ...

Find the similarities and differences between two circular structured JSON objects

How can I effectively compare 2 websocket connections on my nodejs webserver when I can't simply use json.stringify due to the circular structure of the objects? ...

Managing an undetermined quantity of elements from a form in PHP

Currently developing an inventory page for my job. I've crafted a page that will iterate through my database and showcase all the items stored within. include 'auth.php'; //authentication required for login change $sql="SELECT * FROM `inven ...

Shortcuts for $scope variables in AngularJS templates

Within my controller, I often set: $scope.currentThing.data For different parts of my template, sometimes I require currentThing.data.response[0].hello and other times currentThing.data.otherStuff[0].goodbye //or currentThing.data.anotherThing[0].goo ...

Get rid of the span tags and enclose the content within a parent <td> tag

i have this example: <td "class=name"> <span class="removed">one</span> <span class="added">two</span> </td> or maybe this: <td class=name> one <span class="removed">two</span> <sp ...

Setting up express with mongodb - A step-by-step guide

Currently exploring the world of Express App, I am attempting to configure mongodb using express and node.js. Facing a few issues in this process and seeking assistance. I have included directory references for better understanding. 1- The variable config ...

Generating a selection menu for react Select using a one-dimensional array of choices

My challenge lies in transforming an array such as [123, 456] into a react Select component with label and value pairs. The desired "options" structure should have the following format: 0: {label: "123", value: 123} 1: {label: "456", value: 456} I'm ...

What is the best way to combine ExpressJS, VueJS, and Jade for a seamless development

When using ExpressJS, VueJS, and Jade together, what is the recommended approach? Is it necessary to convert Jade files to HTML because VueJS cannot directly serve Jade files? Should I serve a Jade or converted HTML index file in ExpressJS? If I were not ...

Is it possible for me to save external CDN JavaScript files to my local system?

Normally, I would include scripts from other providers in my application like this: <script src="https://apis.google.com/js/api.js"></script> However, I am considering whether it is feasible to simply open the URL , and then copy and paste th ...

Invoke a web service upon the loading of a page within the scope of an AngularJS Framework

My webservice returns a JsonObject and I am calling it in JS using Ajax as shown below : $scope.init = function () { $.ajax({ type: 'GET', timeout: 1000000000, url: serv ...

When invoking the Angular function to make an HTTP request, it will generate a recurring loop

When trying to call a function in mg-repeat that makes an HTTP request with an ID to find a list of data, I encountered an error message. Here is the function call: <div ng-repeat="ListeReponse in reponsefonction(Listechamps.keyQuestion)" > < ...

How can one access a div tag within a script using Jquery?

I am faced with a challenge related to accessing the div tag "warningMessage" within my HTML code. Below is the snippet that contains this div tag under scripts: <script id="notePopup" type="text/x-kendo-template"> <p class="notePopupMessage" ...

Removing files from Dropzone.js uploaded form

For my project, I am utilizing dropzone.js and faced with the task of deleting files from the plugin upload zone (rather than the server) after they have been uploaded. My goal is to revert back to displaying the "Drop files here or click to upload" text o ...

Using object map filtering instead of array filtering in AngularJS

If I have a controller with a $scope property that is an object with other properties rather than an array, how can I properly filter the ng-repeat set in AngularJS? Check out this JSFiddle example: http://jsfiddle.net/ZfGx4/110/ This is how the controll ...

How can I dynamically remove an option from a select dropdown if it already exists in another option using jQuery?

In order to achieve the desired functionality, I need to dynamically adjust the select options based on user input. Additionally, I want the selection to update automatically upon a change event. var dynamicCount = 1; $('#add').click(function ...

What are the best practices for correctly implementing Angularjs ng-repeat?

As a newcomer to angularjs, I am in the process of building an app where the output is already set up correctly. However, I am looking to achieve the same without relying on jQuery, and instead want to utilize the angularjs method using "ng-repeat". Below ...

Having issues with jQuery's .hover and .mouseleave functions causing unintentional looping. Any suggestions on how to resolve this

What I want to achieve: When hovering over the image, it fades out. Text should fade in after the image fades out. Text should fade out when the mouse leaves. The original image should fade back in. End with the original image and no text displayed. Iss ...

HTML dynamic voting system

I'm new to coding in HTML and I have a specific challenge that I need help with. I want to display a value on my webpage that increases every time a user clicks a button. Below is the code I have written so far: <script type="text/javascript& ...

"Presenting Invoice Information on an Invoice Template: A Step-by-Step Guide

Currently, I am working with Laravel 5.7 and VueJs 2.5.*, where I have a table of invoices. My goal is to create a new component that will display a specific invoice based on user selection, allowing them to view or print the chosen invoice. I am still ex ...