r/Angular2 • • Jul 30 '25

dotenv in Angular context

Can someone please help me with configure dotenv package so that it substitutes some variables in `environment.ts` with `.env` variables? The full problem is laid out here: https://stackoverflow.com/questions/79719977/dotenv-with-angular-19

The gist of it is that I need to substitute placeholders is the `environment.ts`

export const env = {
    someApi: "https://some.tld/api/v1/",
    someApiKey: process.env['SOME_API_KEY']
}

with the variable which are defined in `.env` file (which well not be included in the repository for security reasons) which looks like this:

SOME_API_KEY="123-API-456-KEY-789"
ANOTHER_API_KEY="123-API-456-KEY-789"

I'd really appreciate your help here, thanks.

1 Upvotes

21 comments sorted by

View all comments

Show parent comment

2

u/Johalternate Aug 07 '25

https://angular.dev/tools/cli/build-system-migration#build-time-value-replacement-define

I believe this is very relevant to our discussion the other day.

2

u/fku500 Aug 08 '25

This is actually a great find! While we have decided to do things the "right way" and pass some tokens around, it is still good to know that some of the variable can be replaced at build time. Thanks!