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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read