Mastering Angular: Understanding the Importance of Proper Folder Structure
Introduction
One day, while in search of groceries, I visited a recently opened supermarket near my home. However, due to incomplete setup, all the items were scattered and disorganized. Despite having a grocery list, I struggled to locate a single item. Frustrated, I had to resort to purchasing my groceries from another small shop. This experience made me realize the importance of proper organization and how difficult it can be to find items if they are not arranged in a proper manner.
The same principle applies to programming. Without proper project structuring, finding the correct file becomes a challenge as the project grows. Additionally, unclear folder structures can make it difficult for other developers to understand the project’s organization. In this article we will cover how to organize our files in angular application.
When we are creating a new project using angular command line interface, angular provides a default folder structure .
As we add new features to our projects, it becomes necessary to maintain the existing folder structure. There is no fixed or standardized rule for structuring an Angular project, as it can vary significantly depending on the project’s requirements. Typically, the project team determines the folder structure based on the complexity of the application. In this article, we will be discussing a common folder structure that can be applied to both small and large-scale applications.
Similar to how a shopping mall has different sections for displaying items, such as Electronics and Appliances, Home and Furniture, Food and Beverage, Sports and Fitness, Toys and Games, and Books and Stationery, we should categorize similar feature files in one group or folder.
Here are some recommended folders for organizing your application’s code:
1. App Folder: The default module created by the Angular CLI serves as the foundation for the application. It encompasses a majority of the application’s files and plays a crucial role in the overall structure. This module is registered within the app module and is also configured in the main.ts file, which allows Angular to bootstrap the application. In simpler terms, we can consider this module as the entry point to the application, where everything starts.
2. Core Folder: The Core module referred to as the collection of common features that are utilized throughout the application. Typically, these features are loaded only once during runtime, making them singletons. The Core module encompasses functionalities such as authentication handling, error management, and data retrieval. It also includes commonly used components like header, footer, and sidenav. Additionally, the Core module contains interceptors, guards, constants, enums, utils, and universal models that are shared across different parts of the application.
3. Shared Folder: The Shared module is responsible for encapsulating shared components, services, directives, pipes, and modules that can be utilized across various sections of your application. It holds a crucial role in implementing lazy loading. It is important to note that the Shared module should not rely on any specific business logic of the application.
In the Shared module, it is recommended to include commonly used directives, pipes, and components. Additionally, any third-party library components that are frequently utilized should be exposed from this module, allowing easy access and integration throughout the application.
Get Abinash Patnaik’s stories in your inbox
Join Medium for free to get updates from this writer.
4. Modules Folders: As the application expands, it becomes necessary to organize it into separate modules based on features. Each module will have its own dedicated folder where all the components, services, models, and other functionalities related to that specific module will reside. It is advisable to name the folder with the corresponding module name, ensuring clarity and ease of navigation within the project structure.
5. Styles Folder: When creating an Angular application, a global style file named style.scss is generated by default in the root folder. However, it is possible to create additional global styling files that can be utilized throughout the application. Depending on the requirements, separate style files can be created for different segments.
In the current market trend, day and dusk themes are popular choices. As a result, separate global style files can be created for the day theme and the dusk theme, both of which can be placed in a styles folder. This approach ensures that the styling for each theme is organized and easily accessible.
Moreover, if there are different segments within the application that require distinct styles, separate style files can be created for each segment. This allows for modular and targeted styling based on the specific requirements of each segment.
5. Assets Folder: The default folder created by an Angular application is where we can store all the static files such as images, icons, media files, fonts, and static JSON files. It is recommended to create separate folders for each type of static file to ensure a well-organized structure.
For example, you can create a folder specifically for images to store all image files, another folder for icons to keep icon files, and so on. This approach helps maintain clarity and makes it easier to locate and manage static files within the application.
6. Environments Folder: In every application, there are certain configurations that vary based on different environments, such as development, testing, user acceptance testing (UAT), and production. These configurations can include items like database connection strings, API endpoints, or other environment-specific settings. To manage these environment-specific configuration files, we can utilize the “environments” folder.
The “environments” folder is where we store the configuration files corresponding to each environment. For instance, we can have separate files for development, testing, UAT, and production, each containing the specific configuration values for that environment. This allows for easy management and deployment of the application in different environments, ensuring that the appropriate configurations are applied accordingly.
Conclusion
Defining the folder structure of an application is not governed by strict rules but rather depends on the complexity of the project. It is crucial to decide on the folder structure in the early stages of application development, after thorough discussions with the development team.
Creating a well-thought-out folder structure may initially require significant time investment. However, it pays off in the long run, as it streamlines the development process and saves time. A well-organized folder structure enhances code readability, facilitates collaboration among team members, and simplifies maintenance and troubleshooting tasks. Ultimately, investing time in planning and implementing a thoughtful folder structure leads to improved productivity and efficiency throughout the application development lifecycle.
Keep reading, evolve into your best self…





