File and Folder Structure

Introduction

Effective file and folder structure in a JavaScript project is essential for maintaining an organized and manageable codebase. While there’s no one-size-fits-all approach, certain practices like modularity, readability, scalability, and colocation can greatly enhance the project’s structure.

Colocation

Colocation refers to the practice of placing related files close to each other in the directory structure. This reduces the time spent navigating between files and enhances the cohesiveness of components and their associated assets. Colocation is a popular pattern in JavaScript web development and React, where related CSS, JS, and HTML files are grouped together in a single folder.

Basic Structure

How you structure your project depends on its scale and complexity. For a basic static website with minimal JavaScript, you would likely have a few static pages and a main.js file to handle the little JavaScript you need.

For a more complex web application, you would likely have a more elaborate structure with multiple folders and files. In these setups you would typically have a bundler to bundle your JavaScript files into a single file, and a transpiler to convert your modern JavaScript code into a format that is compatible with older browsers. In these cases you’ll likely have a src folder where you’ll keep your source code, and a dist folder where you’ll keep your bundled and transpiled code.

Naming Conventions

It’s important to use a consistent naming convention for your files and folders. This makes it easier to navigate and understand the project. There are several naming conventions you can use, but the most common are:

  • PascalCase: Capitalize the first letter of each word. e.g., HomePage.js. This is typically used for classes and React components.

  • camelCase: Capitalize the first letter of each word except the first. e.g., homePage.js. This is typically used for variables, functions, and files.

  • kebab-case: Separate words with a hyphen. e.g., home-page.js. This is typically used for file names.

  • snake_case: Separate words with an underscore. e.g., home_page.js. This is typically used as a fallback for file names.

Example Structures

We are going to look at some examples of how you could structure your files and folders in a JavaScript project. These are not the only ways to structure a project, but they are some of the most common.

Example 1: Feature-based Structure

Ideal for larger projects with distinct features/modules.

  • src/
    • featureA/
      • components/
      • services/
      • models/
      • featureA.js
    • featureB/
      • components/
      • services/
      • models/
      • featureB.js
    • shared/
      • utils/
      • commonComponents/
    • app.js
    • index.js

Example 2: Layer-based Structure

Suits applications where separation by technical role (e.g., MVC) is preferred.

  • src/
    • controllers/
      • userController.js
      • productController.js
    • models/
      • User.js
      • Product.js
    • views/
      • userView.html
      • productView.html
    • services/
      • userService.js
      • productService.js
    • index.js

Example 3: Domain-driven Structure

For complex business logic, grouping by domain entities can be effective.

  • src/
    • userDomain/
      • userService.js
      • userController.js
      • UserModel.js
    • productDomain/
      • productService.js
      • productController.js
      • ProductModel.js
    • shared/
      • utils.js
    • index.js

Example 4: Colocated Front-end Structure

For front-end projects, colocating related CSS, JS, and HTML can be beneficial. This is a popular pattern in React.

  • src/
    • components/
      • Header/
        • Header.js
        • Header.css
      • Footer/
        • Footer.js
        • Footer.css
    • views/
      • HomePage/
        • HomePage.js
        • HomePage.css
      • AboutPage/
        • AboutPage.js
        • AboutPage.css
    • app.js
    • index.js

Other Considerations

  • Scalability: Choose a structure that can handle the growth of your project.
  • Team Preferences: Consider the preferences and experiences of your team.
  • Project Type: Different project types (e.g., libraries, web applications, services) may benefit from different structures.

Conclusion

There is no single correct way to structure files and folders in a JavaScript project. The choice depends on the project’s scale, complexity, and team preferences. Whether using a feature-based, layer-based, domain-driven, or colocated structure, the goal is to enhance the project’s maintainability and understandability.