Logo

dev-resources.site

for different kinds of informations.

Stop using Dotenv in your front-end

Published at
7/14/2021
Categories
react
webdev
dotenv
Author
sbelzile
Categories
3 categories in total
react
open
webdev
open
dotenv
open
Author
8 person written this
sbelzile
open
Stop using Dotenv in your front-end

Dotenv is wonderful. It allows you to use environment variables in your code, hence separating the code from its running environment.

The problem I have with it is that it is common to see people using it in the front-end of their web application (served static files, non-SSR applications).

What is wrong with this?

Your environment becomes hard-coded in your files at build time. If you proceed this way, you need to build your application every time you wish to deploy it.

Alternative

Load your configuration at runtime, either from a backend or from a configuration file (with an hash in the name). This way, your CI and your CD become 2 independent components. You build only once as opposed to once per deployment. Furthermore, the same artifact is being deployed everywhere, saving you a couple build minutes, and increasing your confidence in what you are deploying.

Implementation example (configuration file)

In your index.html file, add a setting file that contains your environment variables:

<script src="./settings/settings.js"></script>
// must be placed before your js files
Enter fullscreen mode Exit fullscreen mode

File content example:

var environment = {
  "backendBaseUrl": "http://localhost:8000",
}
Enter fullscreen mode Exit fullscreen mode

Using Typescript? Type it:

type Environment = {
  backendBaseUrl: string
}

declare global {
  const environment: Environment
}
Enter fullscreen mode Exit fullscreen mode

Then use it:

const url = `${environment.backendBaseUrl}/potato`
Enter fullscreen mode Exit fullscreen mode

Your deployment pipeline simply needs to ensure this file is being generated/deployed, which is way faster than building your whole codebase.

dotenv Article's
30 articles in total
Favicon
Load Environment Variables using dotenv-local
Favicon
Hashicorp Vault Agent Tutorial: Generating .env from Vault Secrets
Favicon
Flutter Web | Build with .env File
Favicon
Learn .env in Express.js for Beginners (Effortless Setup)
Favicon
How to Hide Only API Keys Instead of Entire Files on GitHub and From Its Commit History
Favicon
Practical Introduction to Environment Variables Using Node.js
Favicon
From dotenv to dotenvx: Next Generation Config Management
Favicon
How to use `.env` file v:20.6.0 `dotenv` npm package do not use.
Favicon
Community Spotlight: David Cochrum
Favicon
Node.js 20.6.0 includes built-in support for .env files
Favicon
What is a .env.vault file
Favicon
Environment variables and configuration anti patterns in Node.js applications
Favicon
Dotenv: Python app environment variable vs. Linux environment variable
Favicon
Node.js includes built-in support for .env files
Favicon
How does python-dotenv simplify Configuration Management?
Favicon
Env::Dot
Favicon
How do you set up .env variables in your NextJS project ?
Favicon
Using ENV file in React & Webpack
Favicon
A simple trick for your dotenv files
Favicon
dotenv and typescript
Favicon
Environment variables & Its best practices
Favicon
Password Manage your environment and secrets with bitwarden
Favicon
5 reasons why your .env environment variables don't work
Favicon
Creating a DotEnv Loader in PHP
Favicon
NextJS - Get rid of DotENV
Favicon
Setting-up a Django project for production
Favicon
Stop using Dotenv in your front-end
Favicon
Supercharge your .env powered projects!
Favicon
Ways to load env variables for your script
Favicon
Doppler: The Triumph and Tragedy of .env Files

Featured ones: