219

I was reading related questions and I found this one, but my question is how can I switch from development to production mode. There are some differences between the modes which are pointed out here.

In the console I can see ....Call enableProdMode() to enable the production mode. However, I'm not sure instance of which type should I call that method on.

Can somebody answer this question?

Alexander Abakumov
  • 12,301
  • 14
  • 79
  • 125
Angel Angel
  • 16,564
  • 27
  • 71
  • 101
  • I found it crazy passing configuration using Webpack 2+ Angular2 and Typescript, created a simple solution: https://github.com/Sweetog/yet-another-angular2-boilerplate – Brian Ogden Feb 24 '17 at 23:12

19 Answers19

232

You enable it by importing and executing the function (before calling bootstrap):

import {enableProdMode} from '@angular/core';

enableProdMode();
bootstrap(....);

But this error is indicator that something is wrong with your bindings, so you shouldn't just dismiss it, but try to figure out why it's happening.

Mark Rajcok
  • 356,856
  • 113
  • 487
  • 490
Sasxa
  • 38,926
  • 16
  • 86
  • 99
107

The best way to enable the production mode for an Angular 2 application, is to use angular-cli and build the application with ng build --prod. This will build the application with production profile. Using angular-cli has the benefit of being able to use development mode using ng serve or ng build while developing without altering the code all the time.

fida1989
  • 3,234
  • 1
  • 26
  • 30
Matthias B
  • 5,355
  • 2
  • 43
  • 46
  • 8
    I'm using Angular 6 and it's ng build --prod to build in prod mode. Just putting this here for any newcomers visiting this page. – Verbose Aug 11 '18 at 03:14
  • I think that _build --prod_ has been implemented keeping isDevMode and enableProdMode in mind. This answer should be marked as the answer – bubi Feb 13 '19 at 07:12
63

This worked for me, using the latest release of Angular 2 (2.0.0-rc.1):

main.ts

import {enableProdMode} from '@angular/core';

enableProdMode();
bootstrap(....);

Here is the function reference from their docs: https://angular.io/api/core/enableProdMode

Pankaj Parkar
  • 130,886
  • 22
  • 223
  • 289
adampasz
  • 1,056
  • 9
  • 9
  • 1
    new link: https://angular.io/docs/ts/latest/api/core/index/enableProdMode-function.html – emp Jul 13 '16 at 13:13
  • 3
    @emp I'm surprised the official Angular documentation for `enableProdMode` doesn't tell you *where* you're supposed to call it. – Dai Feb 26 '18 at 22:58
63

When I built a new project using angular-cli. A file was included called environment.ts. Inside this file is a variable like so.

export const environment = {
  production: true
};

Then in main.ts you have this.

import './polyfills.ts';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';
import { AppModule } from './app/';

if (environment.production) {
  enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule);

You could add this to a non angular-cli project, I would assume, because enableProdMode() is being imported from @angular/core.

howserss
  • 1,100
  • 1
  • 8
  • 12
  • How does this work for non-prod build? I assume in that case environment.ts is ignored, so don't you get a tsc compile error when trying to import the module? – llasarov Dec 08 '16 at 08:31
  • @llasarov the environment.ts is just behaving like a configuration file, in which you can turn on/off production mode. The main.ts just calls the enableProdMode if its true, so this can be done without any specific build process. You can also do selecting logging here, by checking if the logMode is debug then your custom logger service doing a detailed StackTrace else just logging a one liner – NitinSingh Jun 20 '17 at 20:01
14

Go to src/enviroments/enviroments.ts and enable the production mode

export const environment = {
  production: true
};

for Angular 2

11

To enable production mode in angular 6.X.X Just go to environment file

Like this path

Your path: project>\src\environments\environment.ts

Change production: false from :

export const environment = {
  production: false
};

To

export const environment = {
  production: true
};

enter image description here

4b0
  • 20,627
  • 30
  • 92
  • 137
Osama khodrog
  • 1,180
  • 2
  • 22
  • 28
10

ng build --prod replaces environment.ts with environment.prod.ts

ng build --prod

9

Most of the time prod mode is not needed during development time. So our workaround is to only enable it when it is NOT localhost.

In your browsers' main.ts where you define your root AppModule:

const isLocal: boolean = /localhost/.test(document.location.host);
!isLocal && enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

The isLocal can also be used for other purposes like enableTracing for the RouterModule for better debugging stack trace during dev phase.

LeOn - Han Li
  • 8,022
  • 1
  • 57
  • 53
8

In environment.ts file set production to true

export const environment = {
  production: true
};
Vinodh Ram
  • 599
  • 1
  • 8
  • 19
6

When ng build command is used it overwrite environment.ts file

By default when ng build command is used it set dev environment

In order to use production environment, use following command ng build --env=prod

This will enable production mode and automatically update environment.ts file

Waqas Saleem
  • 61
  • 1
  • 1
  • 1
    This was changed in Angular CLI 6 to `ng build --configuration=production` (as default using the CLI-generated angular.json file). – slasky Oct 18 '18 at 02:07
5

you can use in your app.ts || main.ts file

import {enableProdMode} from '@angular/core';
enableProdMode();
bootstrap(....);
Samudrala Ramu
  • 2,008
  • 1
  • 13
  • 35
5

For Angular v12 and onwards ng build --prod has been deprecated.

You should now build it using ng build --configuration=production where again as before

the src/environments/environment.ts file is replaced with the target-specific version of the file, src/environments/environment.prod.ts.

Panagiotis Bougioukos
  • 10,173
  • 2
  • 13
  • 29
4

For those doing the upgrade path without also switching to TypeScript use:

ng.core.enableProdMode()

For me (in javascript) this looks like:

var upgradeAdapter = new ng.upgrade.UpgradeAdapter();
ng.core.enableProdMode()
upgradeAdapter.bootstrap(document.body, ['fooApp']);
Ryan Crews
  • 2,945
  • 1
  • 33
  • 27
4

You don't need any environment.ts or such file to be provided by your seed project. Just have a configuration.ts and add all such entries which require runtime decision (example:- logging configuration and urls). This will fit in any design structure and also help in future

configuration.ts

export class Configuration {

   isInProductionMode : bool = true;

   // other configuration
   serviceUrl : string = "http://myserver/myservice.svc";
   logFileName : string = "...";
}

// Now use in your startup code (main.ts or equivalent as per the seed project design

import { Configuration } from './configuration';
import { enableProdMode } from '@angular/core';
....
if (Configuration.isInProductionMode)
    enableProdMode();
NitinSingh
  • 1,947
  • 1
  • 14
  • 29
3

Just write: ng s --prod

This will serve the project as if you are on prod mode

simi
  • 169
  • 2
  • 6
1

In Angular 10 :

Find the file path ./environments/environment.ts under your 'app' and set 'production' to 'true'.

Before change:

export const environment = {
  production: false
};

After change:

export const environment = {
  production: true
};

I hope it helps you.

Praveen Patel G
  • 176
  • 3
  • 12
1

Just run:

ionic serve --prod

or

ng build --prod

Angular is running in development mode. Call enableProdMode() to enable production mode.

Mario Petrovic
  • 5,868
  • 12
  • 32
  • 54
0

My Angular 2 project doesn't have the "main.ts" file mentioned other answers, but it does have a "boot.ts" file, which seems to be about the same thing. (The difference is probably due to different versions of Angular.)

Adding these two lines after the last import directive in "boot.ts" worked for me:

import { enableProdMode } from "@angular/core";
enableProdMode();
Gyromite
  • 689
  • 5
  • 15
0

before deployment always build in prod mode. this message comes when it the build is done in development mode. in development mode anyone can view the entire angular source code using any browser.

  • Welcome to Stack Overflow. Are you sure this answers the question? OP appears to be asking _how_ do do this, not whether they should. Please read [answer]. – Chris Dec 08 '21 at 21:19