chihab / dotenv-run

Seamlessly load environment variables. Supports cli, esbuild, rollup, vite, webpack, angular. ESM and Monorepos.
246 stars 17 forks source link

NG_APP_ENV not taken from .env file #48

Closed Azel-ytof closed 1 year ago

Azel-ytof commented 1 year ago

Hi,

I face a problem, similar to #45 (I create a new issue because I am not sure that it is exactly the same)

Angular CLI: 16.0.3 Node: 20.0.0 (Unsupported) Package Manager: npm 9.6.4 OS: linux x64 (WSL2)

"@ngx-env/builder": "^16.1.1"

I have a .env file in the root project (next to angular.json, tsconfig.json, ...) It is the only one existing and the content is :

NG_APP_ENV=localhost
NG_APP_API_URL=http://localhost:6000
NG_APP_DISPLAY_URL=http://localhost:7000
NG_APP_CDN_URL=http://localhost:8000

But when I launch app (ng serve), the NG_APP_ENV is not set (but the other are taken)


------- @ngx-env/builder -------
- Verbose:  true
- Prefix:  NG_APP
- Working directory:  /workspaces/myapp
- Environment files: 
 ✔ /workspaces/myapp/.env
- Injected keys:
 ✔ NG_APP_ENV => undefined
 ✔ NG_APP_API_URL
 ✔ NG_APP_CDN_URL
 ✔ NG_APP_DISPLAY_URL
---------------------------------

{
  appEnv: undefined,
  stringified: {
    NG_APP_API_URL: '"http://localhost:6000"',
    NG_APP_CDN_URL: '"http://localhost:8000"',
    NG_APP_DISPLAY_URL: '"http://localhost:7000"',
    NG_APP_ENV: undefined
  }
}

Si I tried to set NG_APP_ENV in command line and it works.


------- @ngx-env/builder -------
- Verbose:  true
- Prefix:  NG_APP
- Working directory:  /workspaces/myapp
- Environment files: 
 ✔ /workspaces/myapp/.env
- Injected keys:
 ✔ NG_APP_ENV => localhost
 ✔ NG_APP_API_URL
 ✔ NG_APP_CDN_URL
 ✔ NG_APP_DISPLAY_URL
 ✔ NG_APP_ENV
---------------------------------

{
  appEnv: 'localhost',
  stringified: {
    NG_APP_API_URL: '"http://localhost:6000"',
    NG_APP_CDN_URL: '"http://localhost:8000"',
    NG_APP_DISPLAY_URL: '"http://localhost:7000"',
    NG_APP_ENV: '"localhost"'
  }
}

So is it an issue that the env is not take from the dotenv file ? Or is it normal and I have to give i with command line each time ? Or maybe I misunderstand something and I have to set NODE_ENV or something else ?

chihab commented 1 year ago

NG_APP_ENV is to be set from the command line indeed as the builder uses its value to determine what environment files it should load besides .env and .env.local. So it doesn't make sense to put it in an environment file.

NG_APP_ENV is set to NODE_ENV by default so doing NODE_ENV=dev npm start is equivalent to doing NG_APP_ENV=dev npm start

Note: If you set NG_APP_ENV to localhost (in command line) the builder will look also for an env file named .env.localhost.

Does it make sense?

Azel-ytof commented 1 year ago

Hi,

Thank you for your answer. Yes it make sense I am currently making the CD of a project and wanted to put all in .env, but I will use a custom variable to set NODE_ENV when running it.