How To Disable And Enabling Production Mode In Angular?
Last Updated :
30 Sep, 2024
In Angular, running an application in production mode is important for optimizing performance and minimizing bundle sizes. Understanding how to switch between production and development modes is essential for Angular developers. This article will explain how to enable and disable production mode in Angular and the effects of doing so.
What is the Production Mode in Angular?
Production Mode in Angular refers to a mode of the application optimized for deployment in a live environment. It differs from the Development Mode in several key ways:
- Optimizations: Production mode enables several performance optimizations, such as Ahead-of-Time (AOT) compilation, minification, and tree-shaking.
- Error Reporting: The development mode includes more detailed error messages and warnings, which are disabled in production mode to enhance performance.
- Change Detection: Angular performs additional checks in development mode to help catch common mistakes. These checks are disabled in production mode to improve runtime efficiency.
By default, Angular applications run in development mode unless explicitly configured otherwise.
There are several ways to switch between development and production modes in Angular, depending on how the application is being built and deployed.
Option 1: Enabling Production Mode through Angular CLI
The simplest and most common way to enable production mode in Angular is by using the Angular CLI during the build process. The Angular CLI has built-in support for switching between development and production configurations.
Step 1: Build for Production
To enable production mode, use the --prod flag when building your Angular application. This flag automatically configures the application for production, enabling AOT compilation, minification, and other optimizations.
ng build --prod
Disable And Enabling Production Mode In AngularWhen you use this command, the following optimizations are automatically applied:
- Ahead-of-Time (AOT) Compilation: Pre-compiles templates and components during the build process.
- Minification: Reduces the size of JavaScript files by removing unnecessary characters.
- Tree Shaking: Eliminates unused code from the final bundle.
- Disable Development Mode: Production mode disables development-specific error checking and assertions, such as console warnings and Angular’s development mode warnings.
You can now deploy the generated files from the dist/ folder to your production environment.
Step 2: Running Angular in Development Mode
To run the application in development mode, simply omit the --prod flag when running or building the application. Development mode will remain enabled.
ng serve
or
ng build
In development mode, Angular includes additional checks and logs that help during the development process. It also provides more verbose error messages to assist with debugging.
Option 2: Manually Enabling Production Mode in Code
In certain scenarios, you may want to explicitly enable production mode in your code, especially if you need to toggle between environments or dynamically manage production mode.
Step 1: Import the enableProdMode Function
To manually enable production mode in your Angular application, import the enableProdMode function from the @angular/core module in your main.ts file.
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
if (environment.production) {
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.error(err));
Step 2: Set the Production Environment Variable
The enableProdMode function is typically called based on the value of the production variable in the environment configuration file. This variable is defined in the src/environments/environment.ts file for development and in src/environments/environment.prod.ts for production.
In the environment.ts file (development environment):
export const environment = {
production: false,
};
In the environment.prod.ts file (production environment):
export const environment = {
production: true,
};
The environment.production variable is automatically set to true or false based on the environment, allowing Angular to conditionally enable production mode. When you run ng build --prod, Angular automatically replaces environment.ts with environment.prod.ts.
Option 3: Modifying the Angular JSON Configuration
Another way to control production and development builds is through the angular.json configuration file. This file defines the default build configurations for different environments.
Step 1: Modify the Angular Configuration
In the angular.json file, locate the configurations section under the build and serve options.
{
"projects": {
"your-app-name": {
"architect": {
"build": {
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true
}
}
},
"serve": {
"configurations": {
"production": {
"browserTarget": "your-app-name:build:production"
}
}
}
}
}
}
}
When you run ng build --prod or ng serve --configuration=production, Angular uses this configuration to apply production mode optimizations.
Option 4: Toggling Production Mode in Runtime
In some advanced cases, you might want to toggle between development and production modes dynamically at runtime. However, this approach is uncommon and typically avoided in production applications because enabling/disabling production mode is expected to be a build-time decision.
Verifying Production Mode
Once production mode is enabled, you can verify whether your application is running in production mode by using the following snippet in the browser console:
console.log(isDevMode());
If isDevMode() returns false, the application is running in production mode. If it returns true, the application is in development mode.
Effects of Enabling Production Mode
- Performance Improvements: Production mode significantly improves performance by disabling Angular’s development-specific error-checking mechanisms and enabling optimizations such as tree shaking and AOT compilation.
- Error Messages: Angular suppresses error messages that are only useful in a development environment. This includes assertions and console warnings.
- Bundle Size: Enabling production mode reduces the size of the final build output by removing unused code and minifying the JavaScript and CSS files.
- Change Detection: In development mode, Angular performs additional checks during change detection to help identify potential errors. These checks are disabled in production mode to speed up the application.
Similar Reads
How to bundle an Angular app for production?
Introduction Before deploying the web app, Angular provides a way to check the behavior of the web application with the help of a few CLI commands. Usually, the ng serves command is used to build, watch, and serve the application from local memory. But for deployment, the behavior of the application
4 min read
How to create module with Routing in Angular 9 ?
Angular applications are modular, and NgModules is Angular's own modular architecture. NgModules are containers for closely integrated application domains, workflows, or feature sets that comprise cohesive code blocks. Their scope is governed by the NgModule they include, and they can contain compon
4 min read
How to Enable or Disable Offline Mode in Google Chrome
Need to browse without internet or save data? Google Chromeâs offline mode lets you access cached pages and continue working even without a connection. This guide shows you how to toggle offline mode easilyâwhether you want to test web apps, conserve mobile data, or read saved articles during flight
3 min read
How to Enable HTML 5 Mode in Angular 1.x ?
HTML5 mode in Angular1.x is the configuration that replaces the default hash-based URLs and provides more user-friendly URLs using the HTML5 History API. This feature mainly enhances our one-page application and also provides a better experience to the users. We need to configure our server with pro
6 min read
How to Create a new module in Angular ?
Modules are most important when it comes to building strong and scalable Angular applications. They help organize your code, promote modularity, and improve maintainability. It encourages collaboration among developers by grouping related components, directives, pipes, and services. In this article,
3 min read
How to Get All Route Params/Data in Angular?
In Angular, managing route parameters and extracting data from routes is important for creating dynamic and responsive web applications. Route parameters allow you to pass information in URLs, and accessing this data enables your application to make decisions and render content dynamically. This art
4 min read
Ant Design Introduction and Installation for Angular
Ant Design is a design pattern for enterprise-level products that can be integrated with other front-end frameworks such as Angular, React, or Vue. Ant Design official implementation is released with React but it can be used with other JavaScript frameworks. It is an open-source tool with approximat
3 min read
MDBootstrap Introduction and Installation for Angular
Introduction: MDBootstrap (Material Design for Bootstrap) is a complete UI package that can be integrated with other frameworks such as Angular, React, Vue, etc. It is used to design a fully responsive and mobile-friendly layout using various components, plugins, animations to make it more attractiv
3 min read
A Complete Guide To Angular Routing
Angular Routing is a technology to build single-page applications that provide multi-page services at a single port. It enables seamless navigation and loading of the different pages. When an Angular app is started then the whole component is only loaded at once and dynamically reloads the requested
6 min read
How to detect a route change in AngularJS ?
In this article, we will see how to detect a route change in AngularJS. In order to detect route change at any moment in AngularJS, this can be achieved by using the $on() method. The $on() method is an event handler, the event which will handle $routeChangeSuccess which gets triggered when route/vi
2 min read