How to check if Angular application running in Production or Development mode

前端 未结 6 607
挽巷
挽巷 2020-12-07 17:27

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?

6条回答
  •  离开以前
    2020-12-07 17:55

    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

    • https://github.com/angular/angular/blob/2.0.0/modules/%40angular/core/src/application_ref.ts#L58

    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') {
      //
    }
    

提交回复
热议问题