206

This seems an easy one, but I couldn't find any solution.

So, how do I check if my app is running in production mode or dev mode?

Gil Epshtain
  • 8,670
  • 7
  • 63
  • 89
maxbellec
  • 16,093
  • 10
  • 36
  • 43

7 Answers7

316

You can use this function isDevMode

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

...
export class AppComponent { 
  constructor() {
    console.log(isDevMode());
  }
}

One note: be carefull with this function

if(isDevMode()) {
  enableProdMode();
}

You will get

Error: Cannot enable prod mode after platform setup

Other options

environment variable

import { environment } from 'src/environments/environment';

if (environment.production) {
  //
}

injected by webpack process.env.NODE_ENV variable

declare let process: any;
const env = process.env.NODE_ENV;

if (env  === 'production') {
  //
}
yurzui
  • 205,937
  • 32
  • 433
  • 399
  • I'm getting the same error you mentioned. "Cannot enable prod mode after platform setup". Could you please help me in getting this resolved? @yurzui – Gowtham Mar 27 '17 at 12:13
  • @Gowtham You have to enable it before calling `platformBrowserDynamic().bootstrapModule(AppModule);` – yurzui Mar 27 '17 at 12:15
  • That is exactly how I'm calling it. Still I'm getting this error every time I try running the app in production mode @yurzui. Any help resolving this would be much appreciated. Thanks – Gowtham Mar 27 '17 at 12:21
  • @Gowtham Do you have any example to reproduce it? – yurzui Mar 27 '17 at 12:29
  • This is my "main.ts" import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { Splashscreen } from 'ionic-native'; import { AppModule } from './app.module'; import {enableProdMode} from 'angular2/core'; enableProdMode(); platformBrowserDynamic().bootstrapModule(AppModule); is it possible for you to resolve this in chatroom? Is this what you wanted? @yurzui – Gowtham Mar 27 '17 at 12:41
  • It works perfectly! I could separate between prod and dev projects in firebase with that method! – Ivan Lencina Jun 27 '18 at 16:54
  • 2
    https://angular.io/api/core/isDevMode "After called once, the value is locked and won't change any more." The answer should include the doc and this warning! – jasie Jul 16 '19 at 10:58
  • Is there something similar for AngularJS? – Faheem Dec 03 '20 at 11:52
  • To improve readability in the console log, prepend some message to the boolean result that is getting logged. Else you only see true or false in the console, which might come from anything. Sample: `console.log("Angular in Development mode: " + isDevMode());` – Michael Nov 10 '21 at 10:02
61

Per the Angular Deployment guide at https://angular.io/guide/deployment#enable-production-mode:

Building for production (or appending the --environment=prod flag) enables production mode Look at the CLI-generated main.ts to see how this works.

main.ts has the following:

import { environment } from './environments/environment';

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

So check environment.production to see if you are in production.

Most likely you do NOT want to call isDevMode(). Per the Angular API documentation at https://angular.io/api/core/isDevMode:

After called once, the value is locked and won't change any more... By default, this is true, unless a user calls enableProdMode before calling this.

I've found that calling isDevMode() from an ng build --prod build always returns true and always locks you into running in dev mode. Instead, check environment.production to see if you are in production. Then you will stay in production mode.

Ron DeSantis
  • 855
  • 7
  • 5
  • 3
    This should be the accepted answer. (Proper doc linking and explanations.) – jasie Jul 16 '19 at 11:00
  • 4
    The fact that the value won't change doesn't imply that you don't want to call it. You shouldn't be switching from dev mode to prod mode and back while your application is running. So when you're trying to determine whether you should enable production mode, the environment variable is the right way to go, but if you have a service that needs to behave a little differently when in dev mode, `isDevMode()` is a perfectly fine way to accomplish that. – StriplingWarrior Sep 10 '19 at 17:31
10

Simply check the production variable present in the environment file, it will be true for production mode and false for development.

import { environment } from 'src/environments/environment';

if (environment.production) {
  // for production
} else {
  // for development
}
Jasmeet Singh
  • 133
  • 1
  • 5
6

it depends on what you are asking...

If you want to know the mode of Angular, as @yurzui said, you need to call { isDevMode } from @angular/core but it can return false only if you call enableProdMode before it.

If you want to know the build environment, in other words, if your app is running minified or not, you need to set a build variable in your build system... Using Webpack, for example, you should have a look at definePlugin.

https://webpack.github.io/docs/list-of-plugins.html#defineplugin

new webpack.DefinePlugin({
  ENV_PRODUCTION: !!process.env.NODE_ENV
});
Community
  • 1
  • 1
Hitmands
  • 13,491
  • 4
  • 34
  • 69
  • I was actually looking for both. I am using webpack (through the angular-cli), where do I add your lines of code? How do I have access to that variable in my typescript files? I would be great if you could update your answer with that – maxbellec Sep 17 '16 at 09:49
  • Following this http://ngcli.github.io/#getting-started-project-structure you should edit the webpack.config.js then follow the link in my answer... – Hitmands Sep 17 '16 at 10:08
  • The link in your post is outdated, heres the correct one for the [Define Plugin](https://webpack.js.org/plugins/define-plugin/) – HostMyBus Sep 20 '19 at 07:52
4
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module'


platformBrowserDynamic().bootstrapModule(AppModule);
enableProdMode();

This was my code, so I got the same error. I just interchanged line 3 and 4. Then the issue is fixed. So before bootstrapping module we should enable --prod mode.

The correct one can be put in this way,

enableProdMode()
platformBrowserDynamic().bootstrapModule(AppModule);
fredyfx
  • 403
  • 10
  • 22
DEEPAN KUMAR
  • 164
  • 2
  • 10
  • 1
    Interestingly, my new Angular 9 app seems to have put these lines (in this order!) into my "main.ts" file already. It seems to be the default now. – Mike Gledhill Jun 02 '20 at 12:28
1

You should be careful that you check the return value of the isDevMode() function.

My setup was failing because i was checking for existence: if (isDevMode) was always true, even in production because i declared it with import { isDevMode } from '@angular/core';.

if (isDevMode()) returned false correctly.

Spikolynn
  • 4,067
  • 2
  • 37
  • 44
  • try to build you app with `ng build --prod=true` via angular cli – Sathish Kumar k k Oct 10 '19 at 10:52
  • 1
    `if ( isDevMode )` only checks if the identifier **isDevMode** is defined, not null, and not empty nor zero. As the identifier was defined in `@angular/core` that **if()** will always return **true**. Now, `if( isDevMode() )` will actually call the function and it will return if it's development environment or not. – Wilfredo Pomier Jan 30 '20 at 21:41
1
declare let process: any;
const env = process.env.NODE_ENV;

export const environment = {  
  apiURL: (env  === 'production') 
    ? 'https://example.com/api'
    : 'http://localhost:8080/api'
};