@jfan29 I would advise you to avoid using expressions inside annotations, because they generate code at compile time (not runtime) when compiling with AOT and that could lead to very weird issues (that I already had the “luck” to experience). I’m thinking your code might be the same case. What you see during the console.log()
is the runtime value, but how it defined the value at compile time I don’t know.
This is something I wrote to a guy that had weird issues when compiling with AOT (when you run with prod, it uses AOT) when defining firebase params retrieved from environment variables from a file that has its path defined during build time (unfortunately, the ngc and consequently aot is also run in the build time, and it was before the webpack phase, causing the error):
The thing is, when angular runs with AOT, the contents in the modules are precompiled when ngc runs (maybe you saw some problems like arrow functions in modules, that you have to use function explicitely, constants and so on)
It seems this pre-compilation uses the default value of ENV (environment.ts), because it happens before webpack runs
ngc started …
ngc finished in 8.56 s …
webpack started …
using DP_ENV:dev
webpack finished in 39.20 s
I even changed environment.ts to environment.prod.ts, and run, it gave me error in the ngc step, because it could not find the file (so the ngc uses the ENV before it was defined correctly)
without aot the modules are not pre-compiled, so the environment is correct
The solution here was instead of defining the variable path in the webpack phase, use a json file to store environment data and copy the raw json file of the correct environment to the correct directory (that has a file environment.ts
) BEFORE the ngc phase.
There is only one environment.ts
that is like:
import { Environment } from './environment.model';
import * as data from './environment.json';
export const ENV: Environment = <any>data;
(the correct json file is moved in the build stage)
This is the repository he created that solved the issue:
I don’t know if your issue is similar, but based on the weird errors you got it seems like it. You should pay attention to where you are exporting your environment variables, and if it will be available at the build stage and when.
As a rule of thumb, I advise to whenever possible avoid variables and functions inside annotations (errors in such cases can be very misleading and hard to find the cause).