Angular environment.ts not loading correctly
Complete Fix Guide
“My environment variables are not being used!” — This is a common Angular issue where the environment file (environment.ts) isn't loading the correct values, especially in production builds. This guide explains why it happens and provides step-by-step fixes for every scenario.
📑 Table of Contents
1. Introduction
Angular applications use environment files to manage configuration that varies between
development, staging, and production. The default setup includes environment.ts
(for development) and environment.prod.ts (for production). However, many developers
encounter issues where the wrong environment file is loaded, variables are undefined, or changes
to the files don't take effect.
This guide covers all the reasons why environment files might not load correctly and provides practical, tested solutions for each. By the end, you'll be able to confidently manage environment configurations in any Angular project.
fileReplacements array in angular.json. If this is misconfigured,
the wrong environment file will be used.
2. Common Causes
- Wrong file replacement configuration –
fileReplacementsnot set correctly in angular.json. - Using the wrong build command –
ng serveuses development,ng build --configuration=productionuses production. - Incorrect import path – Importing from the wrong location, e.g., using
environments/environmentinstead of the correct path. - Missing environment files –
environment.prod.tsor other files are missing. - Cache issues – Browser cache or Angular build cache serving old environment values.
- Custom environment names – Using a custom configuration (e.g., staging) without properly defining file replacements.
- Angular CLI version differences – Behavior changes between Angular versions.
3. File Replacement Configuration
The key to environment loading is the fileReplacements array inside angular.json.
Here’s what a typical configuration looks like:
Important: The src is the file that will be replaced (usually the base environment),
and replace is the file that will be used for that configuration. Ensure both files exist.
✅ Fixing Incorrect Replacements
- Check that the path to both files is correct (relative to the project root).
- Ensure the file names match exactly (case-sensitive on some OS).
- If you have a custom environment, add a new configuration and its file replacement.
- After making changes, restart the build/serve process.
4. Importing Environment
The import path for environment variables is crucial. The standard import is:
If your folder structure is different, adjust the path. Also, ensure that your environment file exports an object with the correct properties.
✅ Correct Environment File
✅ Using Environment in Components
5. Build Configurations
The build command determines which configuration is used:
ng serve– Uses the default development configuration (no file replacement).ng serve --configuration=production– Uses production configuration (replaces environment).ng build– Default build (development).ng build --configuration=production– Production build with file replacement.ng build --configuration=staging– Custom staging build.
If you're serving the app and expecting production values, you need to use the --configuration flag.
angular.json has the correct defaultConfiguration
for the serve target if you want a specific configuration by default.
6. Staging & Custom Environments
Many projects require more than just dev and prod (e.g., staging, qa, testing). Here's how to set up custom environments.
- Create a new environment file, e.g.,
environment.staging.ts. - Add a new configuration in
angular.jsonwith the appropriate file replacement. - Build using
ng build --configuration=staging.
ng serve --configuration=staging to test the staging environment locally.
7. Debugging
- Log the environment: In your
main.tsorapp.component.ts, addconsole.log('Environment:', environment);and check the console. - Check the build output: In the
distfolder, open the main JavaScript file and search for your environment values. They should be hardcoded. - Use source maps: Build with
--source-mapto inspect the original code. - Clear cache: Delete the
.angularfolder andnode_modules/.cacheto ensure a clean build. - Check the
fileReplacementsorder: Angular applies replacements in order, but only one replacement per file is allowed. - Inspect the built file: Use the browser's Developer Tools to view the sources and see which environment file is included.
9. Best Practices
- Keep environment variables minimal: Only store configuration that changes between environments.
- Use a base environment interface: Define an interface for the environment object to ensure type safety.
- Use environment files for secrets (but not sensitive secrets): For real secrets, use environment variables on the server or a secure vault.
- Always commit environment template files: Commit
environment.tsandenvironment.prod.tswith default/placeholder values. Never commit actual secrets. - Use
productionflag wisely: Theproductionproperty is used by Angular to enable production mode. Ensure it's set correctly. - Test environment loading: Write a simple test to verify that the correct environment is loaded in each build.
- Document custom environments: If you add new environments, document the build commands and configurations.
- Use Angular CLI's
ng config: You can useng configto view and modify the configuration from the command line.

0 Comments
thanks for your comments!