environment.dev|prod|etc.ts files don't appear to be copied into environment.ts
Please provide us with the following information:
- OS? Windows 7, 8 or 10. Linux (which distribution). Mac OSX (Yosemite? El Capitan?) Ubuntu 16.04
- Versions. Please run
ng --version. If there's nothing outputted, please run in a Terminal:node --versionand paste the result here:
» ng version
Could not start watchman; falling back to NodeWatcher for file system events.
Visit http://ember-cli.com/user-guide/#watchman for more info.
angular-cli: 1.0.0-beta.11-webpack.8
node: 6.3.0
os: linux x64
- Repro steps. Was this an app that wasn't created using the CLI? What change did you
do on your code? etc.
- Add some custom fields/data in environment.dev.ts (or environment.prod.ts)
- Import
src/environments/environmentinto a component and attempt to use a newly added variable. - ng serve/build with the appropriate env set.
- Variable will not have been read, as environment.ts will still be
{ production: bool }
- The log given by the failure. Normally this include a stack trace and some more information. N/A
- Mention any other details that might be useful. Specifically importing environment.dev or environment.prod (as appropriate) works fine, but obviously isn't practical.
Thanks! We'll be in touch soon.
See #2073. It is changed to be only two files by default environment.ts (dev env) and environment.prod.ts (prod env) You should add your variables to this two files. I think this issue can be closed with this pull request.
I'm a little confused. If environment.dev.ts or environment.prod.ts aren't copied into environment.ts on rebuild, how are we then supposed to import the correct environment file into our code?
Bear in mind that the content will be copied at runtime, in memory for ng serve and to the output bundles in ng build. Your src/environments/environment.ts file will never be altered, only the generated code.
I just tried this on a brand new beta.14 app with ng serve --prod, and as far as I can tell it works properly:

The problem with not actually overwriting the files is that trying to use environment.test throws a compilation error because environment.ts which we import doesn't actually have a test variable in it.

I think I know what's happening. It's the typechecker, it's not using the file replacement. I'll see if it can be fixed.
Try this for now: use the same interface in both files aka have the test object member in both. That should satisfy the typechecker and at runtime you will get the different content:
Example:
// environment.ts
export const environment = {
production: true,
test: 'hello'
};
// environment.prod.ts
export const environment = {
production: true,
test: 'goodbye'
};
ng servewill printhelloinconsole.log(environment.test)ng serve --prodwill printgoodbyeinconsole.log(environment.test)
Thanks for looking into it!
When can we expect a fix for this please?
@ukaaa @radoslavpetranov What is the use case to have different variables in environment? How do you handle that in code?
@JiriBalcar to only show information and links when we're running our app in a development environment.
@ukaaa And for that usecase you can use the production flag which is in both of the files. So you do not need different interface in evironment. Right?
@JiriBalcar oh yes, definitely. Only that doesn't seem to work either for me using [email protected] (both global and local.)
I have the following environments under src/environments:
environment.dev.ts
export const environment = {
production: false
};
environment.prod.ts
export const environment = {
production: true
};
environment.ts
// The file contents for the current environment will overwrite these during build.
// The build system defaults to the dev environment which uses `environment.ts`, but if you do
// `ng build --env=prod` then `environment.prod.ts` will be used instead.
// The list of which env maps to which file can be found in `angular-cli.json`.
export const environment = {
production: false
};
angular-cli.json
"environments": {
"source": "environments/environment.ts",
"dev": "environments/environment.dev.ts",
"prod": "environments/environment.prod.ts"
}
And finally in one of my components let's say I have this:
import { environment } from '../../environments/environment';
But when I serve using ng serve --environment=prod or ng serve --prod and then log what's in the environment variable I get:
{
production: false
}
Same happens with ng build.
@JiriBalcar my use case is slightly different. I think it makes sense to put all possible config variables in a single file (let's say environment.dev.ts) and then have environment.prod.ts basically import environment.dev.ts and extend it (Object.assign) with only those values we need to set different from defaults. This way all supported config values and their defaults are in one place and in the other file we only change the ones we care about without worrying about adding new config values to both files.
For that reason I tried importing environment.dev.ts into environment.prod.ts where I would just extend the defaults in and return the extended environment.dev.ts object. I expected that the end result would end up in environment.ts but then this thread made me realize this is not how it works. All in all at first I thought this is a bug but then I realized that's by design hence my participation in this thread.
@ukaaa Strange, for me this exact scenario works great. Did you tried that in new project? ng new
@radoslavpetranov I beleive it should work if you put your dev config to environment.ts instead of environment.dev.ts as this pull request (#2073). Then you always have full development variables and in prod mode the extend the dev. Did you tried that?
@JiriBalcar With a new project it seems to work … Very strange. Not sure what I should do now.
I've just upgraded my existing project to [email protected] but it still doesn't work.
@ukaaa Can I look at your repository?
@JiriBalcar I did a clean install of the project and now everything works. Probably some files remained somewhere they didn't need to be. I remember the environments folder moving around a few versions back, I guess it was related to that.
I'm having the same problem. On new project is working but on old one is not. Any suggestions?
@Kosmonaft the environments folder is now on a different location than in earlier versions. Maybe you can compare both projects and their folder structure.
I have the similar issue, but simple to solve just looking how it works in new created by angular-cli project. Without creating new project, you can fix it in existing one this way:
- Move
./src/app/environmentsfolder as./src/environments - Move
./src/environments/environment.dev.tsas./src/environments/environment.tswith replacement (don't forget to merge your configs before) - In
./angular-cli.jsonfile edit environments section as it is in new project:
"environments": {
"source": "environments/environment.ts",
"dev": "environments/environment.ts",
"prod": "environments/environment.prod.ts"
}
Worked for me.