Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Managing environment variables in Angular apps
Dimitris Kiriakakis
Dimitris Kiriakakis

Posted on

     

Managing environment variables in Angular apps

Some days ago I had to initialise a newAngular app for a side project of mine. The use case would be fairly simple, and I needed some basic features like user authentication and some entities to store on a backend.

In such casesSupabase is a great open-source alternative to setting up a custom backend, and integrating it into an Angular app is fairly simple, given the existing supabase dependencies like@supabase/supabase-js. The only prerequisite to make it work isinitialising a supabase project andgenerating an API key.

However, I found out that managing environment variables in an Angular project is not really straight forward. Once we generate a project via the Angular CLI, we get theenvironment.ts andenvironment.prod.ts files, but these cannot be used for declaring such API keys, especially if we want to push our codebase to a github repository.

After some research I did, I discovered@ngx-env/builder which, as it seems, provides us with a clean and secure way to store our environment variables.

You can find a sample project showcasing how it works here:

GitHub logo dimeloper / angular-environment-variables

A sample Angular app that showcases how to set up env variables for different environments.

Angular Environment Variables

A sample Angular app that showcases how@ngx-env/builder can be used to introduce Environment Variables and different app environments within an Angular application.

Add package and adjust angular configuration

  • open a terminal
  • go to the project root folder
  • executeng add @ngx-env/builder

Define Environment variables

  • create.env file in your local project directory
  • add it in your.gitignore
  • extend theEnv interface withinenv.d.ts so that the new variables and their types are added belowNODE_ENV (e.g.readonly NG_APP_ENV: string;)
  • add your environment variables and their values within your local.env file with the following format:
NG_APP_PUBLIC_SUPABASE_URL=https://test.supabase.coNG_APP_PUBLIC_SUPABASE_ANON_KEY=keykeykeyvaluevalue
Enter fullscreen modeExit fullscreen mode

Note that all the environment variables should be prefixed withNG_.

Differentiate environments across serve and build tasks

You could possible use the defaultNODE_ENV values to differentiate between e.g.development andproduction environments, however I prefer having my own app environment values…

Getting started

Once we are at our Angular project root directory we can simply runng add @ngx-env/builder to get started. This command will install the necessary dependency, adjust the application builders accordingly and generate the environment types withinenv.d.ts.

Once this is complete we can extend the generatedenv.d.ts and its Env interface with our expected environment variables. For example:

declareinterfaceEnv{readonlyNODE_ENV:string;// Replace the following with your own environment variables.readonlyNG_APP_ENV:string;readonlyNG_APP_PUBLIC_SUPABASE_URL:string;readonlyNG_APP_PUBLIC_SUPABASE_ANON_KEY:string;}
Enter fullscreen modeExit fullscreen mode

Please keep in mind that theNG_APP_ prefixis mandatory. In case you need to change it please consult the relateddocumentation.

Defining Environment Variables

Now we can use.env files on our local and production environments with the corresponding values. First we need to make sure that our.gitignore includes the.env file. Then we can create our local.env file as such:

NG_APP_PUBLIC_SUPABASE_URL=https://test.supabase.coNG_APP_PUBLIC_SUPABASE_ANON_KEY=keykeykeyvaluevalue
Enter fullscreen modeExit fullscreen mode

Since we included this file as part of our.gitignore, these values won't be pushed to our github repository.

Usage in components / templates

We can import the actual values of our environment variables in our components as such:

supabaseUrl=import.meta.env.NG_APP_PUBLIC_SUPABASE_URL;
Enter fullscreen modeExit fullscreen mode

Basically theimport.meta.env object will include all the environment variables we've defined.

Differentiate environments across serve and build tasks

We could possibly use the defaultNODE_ENV values to differentiate between e.g.development andproduction environments, however I prefer having my own app environment values which for this sample project, I've defined withinpackage.json.

Example:

"scripts":{"start:dev":"NG_APP_ENV=dev ng serve","start:staging":"NG_APP_ENV=staging ng serve","start:production":"NG_APP_ENV=production ng serve","build":"NG_APP_ENV=production ng build"}
Enter fullscreen modeExit fullscreen mode

By doing this we have aclear app environment separation, while we are also able to differentiate pieces of implementation depending on the app environment, e.g. if we want to show a teaser component only on production or staging.

Deployment / usage in production

Once our local setup is complete, we can overwrite the values of our environment variables, just by defining them on the staging/production system environment (e.g.export NG_APP_PUBLIC_SUPABASE_URL="environment.specific.url"), or by creating an.env file in there, including all the enviroment specific values (e.g. staging values on the testsystem, production values on the live server).

Conclusion

Managing API keys and secret values that should be handled as environment variables is an essential part of any frontend application that should be production ready. The@ngx-env/builder package provides us with an elegant and straightforward way to do so, when it comes to Angular apps. Enjoy!

Top comments(4)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
erik_k_e28fc68ec86dfb93c4 profile image
Erik K
  • Joined
• Edited on• Edited

Thanks for your help! I was wondering if you still managed to get your browser to auto refresh after using the@ngx-env/builder:application builder in your angular.json file. It seems I need to re-runng serve ... each time I make edits to my typescript code.

CollapseExpand
 
dimeloper profile image
Dimitris Kiriakakis
Hi! My name is Dimitris! 👨🏼‍💻 Fullstack Developer • Freelancer 🛠️ Working with Javascript, TypeScript, Angular, Next.js, Node, NestJS, GoLang, Vue ✌🏻Made in 🇬🇷
  • Location
    Hamburg, Germany
  • Education
    BSc and MSc in Electronics & Computer Engineering
  • Work
    Fullstack Developer @ ZEAL | Freelancer
  • Joined

Hey there, you're welcome. The live reload mechanism works fine in the projects I've used the env builders, whenever I edit a typescript file. Can you doublecheck that in the"serve" configuration within yourangular.json the@ngx-env/builder:dev-server is declared as builder?

CollapseExpand
 
erik_k_e28fc68ec86dfb93c4 profile image
Erik K
  • Joined

No worries. I ended up solving the issue. I used “ng update” to update angular from version 17 to version 18. The ngx-env/builder was on version 18

Thread Thread
 
dimeloper profile image
Dimitris Kiriakakis
Hi! My name is Dimitris! 👨🏼‍💻 Fullstack Developer • Freelancer 🛠️ Working with Javascript, TypeScript, Angular, Next.js, Node, NestJS, GoLang, Vue ✌🏻Made in 🇬🇷
  • Location
    Hamburg, Germany
  • Education
    BSc and MSc in Electronics & Computer Engineering
  • Work
    Fullstack Developer @ ZEAL | Freelancer
  • Joined

Nice, glad to hear you manage to sort it out!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Hi! My name is Dimitris! 👨🏼‍💻 Fullstack Developer • Freelancer 🛠️ Working with Javascript, TypeScript, Angular, Next.js, Node, NestJS, GoLang, Vue ✌🏻Made in 🇬🇷
  • Location
    Hamburg, Germany
  • Education
    BSc and MSc in Electronics & Computer Engineering
  • Work
    Fullstack Developer @ ZEAL | Freelancer
  • Joined

More fromDimitris Kiriakakis

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp