angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

environment.dev|prod|etc.ts files don't appear to be copied into environment.ts

Open rjcoupe opened this issue 10 years ago • 29 comments

Please provide us with the following information:

  1. OS? Windows 7, 8 or 10. Linux (which distribution). Mac OSX (Yosemite? El Capitan?) Ubuntu 16.04
  2. Versions. Please run ng --version. If there's nothing outputted, please run in a Terminal: node --version and 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
  1. Repro steps. Was this an app that wasn't created using the CLI? What change did you do on your code? etc.
    1. Add some custom fields/data in environment.dev.ts (or environment.prod.ts)
    2. Import src/environments/environment into a component and attempt to use a newly added variable.
    3. ng serve/build with the appropriate env set.
    4. Variable will not have been read, as environment.ts will still be { production: bool }
  2. The log given by the failure. Normally this include a stack trace and some more information. N/A
  3. 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.

rjcoupe avatar Sep 12 '16 21:09 rjcoupe

My environment.dev|etc.ts files don't appear to be copied into environment.ts when using AoT AND using components that are AoT compiled/imported from another library...

I had an auth service class that is injected into a lot of various components throughout my app. It's a singleton defined once in the Core Module (per ng docs example). Previously, the class had an import { environment } from "../path/to/environment.ts" in it, which then allowed it different env's API, etc.

Now, I've just refactored this class to another beta.22 / ng 2.4.1 repository, which serves as a common components library for the x other angular app I'll be writing. It is compiled using ngc so my first (and future) app can import it.

Since the service class can no longer depend on a locally imported environment.ts file to inform it of API endpoints, I am instead injecting a config object into the class. (per ng doc's tutorial on DI, OpaqueToken...). This config object is defined in the Core Module instead. And in my Core Module code, I no longer importing auth service locally. This is essentially how it looks:

import { AuthService, AUTH_CONFIG } from "my-common-lib/dist";
import { environment } from "../local/path/to/environment.ts";
@NgModule({
providers: [ 
   { provide: AUTH_CONFIG, useValue: { api:  environment.authApi },
   AuthService
],
})

Works fine so far. Now, I've also refactored out all my auth-related pages, like login, sign-up, reset password pages. These are all packed together into an NgModule and lazy loaded. I successfully refactored it out into the common repo as well:

Routes:

 path: '/my-auth-pages-route',
 loadChildren: "app/auth/module#AuthModule" ...

app/auth/module.ts:

import { AuthPagesModule } from "my-common-lib"
@NgModule({
  imports: [AuthPagesModule],
})
export class AuthModule { }

(in the above file, simply doing an export { AuthModule } from 'my-common-lib' works in normal but do not compile in AoT. I may open another issue for that)

If I serve the app it in normal mode and using the imported pages, the env is set. If I serve the app in AoT, but use pages defined locally, the env is set.

It is only when I do the import AND use AoT that the env is not set. More specifically, a console.log right before the useValue show that the imported env is correct. But, milliseconds later in the console, in the root component constructor, logging the authService's config shows that the env values are the dummy one.

aaronleestic avatar Jan 12 '17 00:01 aaronleestic

@aaronleeucla I might be seeing the same thing. I have a string provider like this:

{provide: 'ApiUrl', useValue: environment.apiurl}

And in my constructor, I print out that the value in the environment is different than the one in the injected provider.

constructor(@Inject('ApiUrl') private csConsoleUrl:string) {
    console.log("Handed ApiUrl provider of " + csConsoleUrl + " vs " + environment.apiurl);
}

This is only during build (which I am doing with AOT). I guess the environment value is being set during compilation prior to the environment being worked out?

pfleeter avatar Jan 16 '17 20:01 pfleeter

@aaronleeucla @pfleeter the issued tied to providing the environment to the DI is logged here. https://github.com/angular/angular-cli/issues/2673. The workaround for now is to provide a factory instead of providing a value

deebloo avatar Jan 16 '17 21:01 deebloo

@deebloo @aaronleeucla Thanks. I wasn't aware of useFactory. Worked perfectly.

pfleeter avatar Jan 16 '17 21:01 pfleeter

hi, how to configure environment variables in angular2 without CLI

RonitMaity avatar Mar 01 '17 18:03 RonitMaity

Can be closed? I believe this was fixed awhile ago

Toxicable avatar Mar 03 '17 03:03 Toxicable

for me it also happens in 1.0.0-rc.0 but it was fixed for me by adding the missing typings

environment.d.ts
interface Environment {
    production: boolean;
    someValue?: {
        nestedProp: string;
    };
}
environment.ts
export const environment: Environment = {
    production: false,
    someValue: {
      nestedProp: 'my value'
    }
}
environment.prod.ts
export const environment: Environment = {
    production: true
}

but: is that really desired / the correct solution?

etwillbefine avatar Mar 10 '17 10:03 etwillbefine

Closing as fixed.

filipesilva avatar May 22 '17 11:05 filipesilva

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.