Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple env files log correctly but have a different effect #100

Open
atollk opened this issue May 31, 2024 · 4 comments
Open

Multiple env files log correctly but have a different effect #100

atollk opened this issue May 31, 2024 · 4 comments
Assignees
Labels
repo requested Reproducing repository is requested

Comments

@atollk
Copy link

atollk commented May 31, 2024

Hi. First of all, thanks for creating the project and keeping it active. It's a good addition that Angular is lacking.

I am trying to inject different configurations into my Angular application with @ngx-env/builder. However, it seems that the env files are not loaded correctly. I am running myapp:serve:development. This is the output when building:

---------------------------------
- Root directory:  /home/x/Projects/git/myapp/web
- Working directory:  /home/x/Projects/git/myapp/web/apps/myapp
- Files: .env
- Environment: none
- Environment files: 
 ✔ /home/x/Projects/git/myapp/web/.env
- Environment variables: NG_APP 
 ✔ NG_APP_BACKEND_API_URL
---------------------------------

Seems valid to me. However, NG_APP_BACKEND_API_URL does not actually have the value that is defined in .env, as it is suggested by that console log, but rather the value that is defined in a separate .env.development. If I delete the latter, then the correct value is used.

On the other hand, if I run NODE_ENV=devxx myapp:serve:development, this is the output:

---------------------------------
- Root directory:  /home/x/Projects/git/myapp/web
- Working directory:  /home/x/Projects/git/myapp/web/apps/myapp
- Files: .env
- Environment: devxx
- Environment files: 
 ✔ /home/x/Projects/git/myapp/web/.env.devxx
 ✔ /home/x/Projects/git/myapp/web/.env
- Environment variables: NG_APP 
 ✔ NG_APP_BACKEND_API_URL
---------------------------------

Again, completely reasonable and what I would expect. But again, that's actually not showing the truth: env.devxx is completely ignored and the values from .env are used.

So, apparently NODE_ENV is used only for logging but for the actual values, the angular build configuration is used?

@chihab chihab self-assigned this May 31, 2024
@chihab
Copy link
Owner

chihab commented May 31, 2024

What version of the package are you using please?

@atollk
Copy link
Author

atollk commented Jun 2, 2024

I'm using 17.3.0.

@chihab
Copy link
Owner

chihab commented Jun 2, 2024

The environment follows this rule:

    process.env.NG_APP_ENV || // @deprecated
    process.env.NODE_ENV || // default in @dotenv-run/core
    builderConfiguration // the angular build configuration

Example: if you set NODE_ENV the builder configuration is ignored
if NG_APP_ENV and NODE_ENV are not set, then the builder configuration is picked.

Would it be possible to provide a repo reproducing the issue (you can fork it on stackblitz from here: https://stackblitz.com/edit/ngx-env-3ey8js)

Which builder are you using: application, browser-esbuild or browser?

@chihab chihab added the repo requested Reproducing repository is requested label Jun 3, 2024
@chihab chihab closed this as completed Jun 16, 2024
@atollk
Copy link
Author

atollk commented Jun 16, 2024

Sorry, I didn't have the time to build a new example from the Stackblitz. I tried to reduce my own project to a small reproducible example.

web.zip

I am running export NODE_ENV=production && npx nx run chess2-web:serve:development. The builder output is what would be intended:

- Files: .env
- Environment: production
- Environment files: 
 ✔ /home/x/Projects/git/chess-2.0/web/.env.production
 ✔ /home/x/Projects/git/chess-2.0/web/.env
- Environment variables: NG_APP 
 ✔ NG_APP_FOO
---------------------------------

But the page is rendered as:

NG_APP_FOO: DEV
NG_APP_ENV:
NODE_ENV: development

So to me it seems like there are two issues here:

  1. At some point, the NODE_ENV variable is just overridden. Since you also said that NG_APP_ENV is deprecated, I wouldn't know how to then manually set my environment.
  2. Even though NODE_ENV is overridden, the builder output seems to print its information before that and "pretends" to use the production environment.

@chihab chihab reopened this Jun 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
repo requested Reproducing repository is requested
Projects
None yet
Development

No branches or pull requests

2 participants