chihab / dotenv-run

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

ERROR TypeError: import.meta.env is undefined - not being replaced when building? #92

Closed davisriska closed 5 months ago

davisriska commented 5 months ago

I followed the quick start guide - https://github.com/chihab/dotenv-run/tree/main/packages/angular Installed with ng add @ngx-env/builder. Moved env.d.ts from /src to /resources/frontend because it wasn't respecting the configured "sourceRoot": "resources/frontend", in angular.json. Removed the depricated process code from env.d.ts because I plan on using import.meta.env method. I see that the builder was replaced in angular.json to builder": "@ngx-env/builder:application",. Im running angular build with npm run watch which is configured as ng build --watch --configuration development. It builds fine but trying to load the angular app i get hit with the error in console - ERROR TypeError: import.meta.env is undefined. I was trying just your sample code

if (import.meta.env.NG_APP_ENV !== "production") {

        }

And when I look at the generated code i see

if (import.meta.env.NG_APP_ENV !== "production") {
}

This is what i recieve as ouput

- Working directory:  /var/www/html/resources/frontend
- Files: .env
- Environment: development
- Environment files: none

This is what I have configured as extras in the options.

"architect": {
                "build": {
                    "builder": "@ngx-env/builder:application",
                    "options": {
                        "ngxEnv": {
                            "verbose": true
                        },
                        "outputPath": "public/dist",
.......

Also I do have

environments.ts files in /resources/frontend/environments and this configuration -

"configurations": {
                        .........
                        "development": {
                            "optimization": false,
                            "extractLicenses": false,
                            "sourceMap": true,
                            "fileReplacements": [
                                {
                                    "replace": "resources/frontend/environments/environment.ts",
                                    "with": "resources/frontend/environments/environment.development.ts"
                                }
                            ]
                        }

Should these files show up under environment files in the output?

What seems weird to me is that I saw in other issues that it outputs verbose: true if its configured and it seems I dont get that. So maybe somethings wrong with the config?

I have restarted everything, so Im pretty sure it should be running with the library. What could I have missed or what should I try to change to figure out why its not working?

Edit: After looking over the angular.json file, I changed the root parameter to "./", so its like this

            "root": "./",
            "sourceRoot": "resources/frontend",

And now this is the output

- Working directory:  /var/www/html
- Files: .env
- Environment: development
- Environment files: 
✔ /var/www/html/.env

So I guess now it finds the .env file I have. But still it gives the same console error previously mentioned.

Edit 2:

sail@82bd2d20941f:/var/www/html$ npm list webpack @angular/core @ngx-env/builder
frontend@0.0.0 /var/www/html
+-- @angular-devkit/build-angular@17.2.3
| +-- @angular-devkit/build-webpack@0.1702.3
| | `-- webpack@5.90.1 deduped
| +-- @ngtools/webpack@17.2.3
| | `-- webpack@5.90.1 deduped
| +-- babel-loader@9.1.3
| | `-- webpack@5.90.1 deduped
| +-- copy-webpack-plugin@11.0.0
| | `-- webpack@5.90.1 deduped
| +-- css-loader@6.10.0
| | `-- webpack@5.90.1 deduped
| +-- less-loader@11.1.0
| | `-- webpack@5.90.1 deduped
| +-- mini-css-extract-plugin@2.8.0
| | `-- webpack@5.90.1 deduped
| +-- postcss-loader@8.1.0
| | `-- webpack@5.90.1 deduped
| +-- sass-loader@14.1.0
| | `-- webpack@5.90.1 deduped
| +-- source-map-loader@5.0.0
| | `-- webpack@5.90.1 deduped
| +-- webpack-dev-middleware@6.1.1
| | `-- webpack@5.90.1 deduped
| +-- webpack-dev-server@4.15.1
| | +-- webpack-dev-middleware@5.3.3
| | | `-- webpack@5.90.1 deduped
| | `-- webpack@5.90.1 deduped
| +-- webpack-subresource-integrity@5.1.0
| | `-- webpack@5.90.1 deduped
| `-- webpack@5.90.1
|   `-- terser-webpack-plugin@5.3.10
|     `-- webpack@5.90.1 deduped
+-- @angular/animations@17.2.4
| `-- @angular/core@17.2.4 deduped
+-- @angular/cdk@17.2.2
| `-- @angular/core@17.2.4 deduped
+-- @angular/common@17.2.4
| `-- @angular/core@17.2.4 deduped
+-- @angular/compiler@17.2.4
| `-- @angular/core@17.2.4 deduped
+-- @angular/core@17.2.4
+-- @angular/forms@17.2.4
| `-- @angular/core@17.2.4 deduped
+-- @angular/material@17.2.2
| `-- @angular/core@17.2.4 deduped
+-- @angular/platform-browser-dynamic@17.2.4
| `-- @angular/core@17.2.4 deduped
+-- @angular/platform-browser@17.2.4
| `-- @angular/core@17.2.4 deduped
+-- @angular/router@17.2.4
| `-- @angular/core@17.2.4 deduped
+-- @asymmetrik/ngx-leaflet@17.0.0
| `-- @angular/core@17.2.4 deduped
+-- @ng-matero/extensions-luxon-adapter@17.1.2
| +-- @angular/core@17.2.4 deduped
| `-- @angular/material-luxon-adapter@17.2.2
|   `-- @angular/core@17.2.4 deduped
+-- @ng-matero/extensions@17.1.2
| +-- @angular/core@17.2.4 deduped
| +-- @ng-select/ng-select@12.0.7
| | `-- @angular/core@17.2.4 deduped
| `-- ngx-color@9.0.0
|   `-- @angular/core@17.2.4 deduped
+-- @ngx-env/builder@17.2.3
| `-- @dotenv-run/webpack@1.3.4
|   `-- webpack@5.90.1 deduped
+-- ngx-progressbar@11.1.0
| `-- @angular/core@17.2.4 deduped
`-- ngx-toastr@18.0.0
  `-- @angular/core@17.2.4 deduped
davisriska commented 5 months ago

I realized that I have to define NG_APP_ENV=wateverhere for it to replace in code. For some reason I expected it to just work and take the value from --configuration or just replace it with an empty string. But I realize now it probably just goes through all available env vars and replaces them and if you have something that dosnt have the value it just leaves the import.meta.env in code.

Is this the expected behavior? As In transpile successfully with variables missing? It would be nice if it would shout warnings at you if theres no configured values.