Community for developers to learn, share their programming knowledge. Register!
React Project Structure

Core Directories in a React Project


If you're looking to understand the core directories in a React project, you've come to the right place. You can get training on this topic through our in-depth guide, which aims to help intermediate and professional developers better structure their React applications. Building a React project isn’t just about writing components; it's about organizing your codebase effectively for scalability, maintainability, and performance. This article takes a closer look at the key directories in a React project and how they contribute to the overall development process.

The node_modules Directory

The node_modules directory is a fundamental part of any JavaScript project, including React. It contains all the dependencies your project needs, downloaded via the npm or yarn package managers. While this folder might seem like a mysterious black box to some, understanding its purpose is crucial.

When you install a package, such as react or react-dom, its source code, along with any of its dependencies, gets stored in node_modules. This directory can grow quite large, especially in bigger projects, because it contains not only the primary libraries but also their sub-dependencies.

One thing to keep in mind is that node_modules should never be manually edited. If you encounter issues, it's best to delete the folder and reinstall dependencies using npm install or yarn install. Also, this folder is excluded from version control systems like Git by adding it to the .gitignore file.

The public Directory: Serving Static Assets

The public directory is where you store static assets for your application. These assets might include images, fonts, or even specialized files like favicon.ico and manifest.json. Files in this directory are served as-is, without being processed by Webpack or Babel.

For example, if you include an image named logo.png in the public folder, you can reference it directly in your app using the absolute URL /logo.png. This is particularly useful for assets that don’t need to be dynamically imported or transformed.

<img src="/logo.png" alt="Logo" />

Additionally, the index.html file in the public folder acts as the starting point of your React application. React attaches its virtual DOM to the <div id="root"></div> element in this file. If you're customizing this file, be cautious, as it will affect the entire app.

The src Directory: Where the Magic Happens

The src directory is the heart of your React project. This is where your application's code lives, including components, styles, utilities, and more. A typical src folder might look something like this:

src/
β”œβ”€β”€ assets/
β”œβ”€β”€ components/
β”œβ”€β”€ pages/
β”œβ”€β”€ styles/
β”œβ”€β”€ App.js
β”œβ”€β”€ index.js

Key Files

  • index.js: This is the entry point for your React application. It renders the root component (App) into the DOM.
  • App.js: The main component that acts as the parent for other components.

Organizing the src Directory

A well-structured src directory can significantly improve your project's readability and maintainability. For instance:

  • Store reusable components in a components/ folder.
  • Group related assets like images and fonts in an assets/ folder.
  • Use a pages/ folder for route-specific components in apps with routing.

By keeping your src folder organized, you make it easier for other developers (and your future self) to navigate the codebase.

Configuration Files: package.json and .env

package.json

The package.json file is the blueprint of your React project. It lists the project's dependencies, scripts, and metadata. For example:

{
  "name": "my-react-app",
  "version": "1.0.0",
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test"
  },
  "dependencies": {
    "react": "^18.0.0",
    "react-dom": "^18.0.0"
  }
}

You can add custom scripts or dependencies here to tailor your project to specific needs.

.env

The .env file is used for managing environment variables. For example, you might define API keys or base URLs:

REACT_APP_API_URL=https://api.example.com

React only exposes variables prefixed with REACT_APP_ to the application. This keeps sensitive data secure and minimizes accidental exposure.

Testing Directory: Organizing Tests in React

Testing is a critical part of any professional React project. While there’s no default __tests__ directory in React, many developers create one to keep test files organized. Alternatively, you might place test files alongside the code they’re testing, using a .test.js or .spec.js naming convention.

For instance, if you have a component named Button.js, you can create a test file like this:

components/
β”œβ”€β”€ Button.js
β”œβ”€β”€ Button.test.js

React testing libraries, such as Jest and React Testing Library, are often used together to write unit and integration tests. Here's a simple example:

import { render, screen } from '@testing-library/react';
import Button from './Button';

test('renders a button with text', () => {
  render(<Button>Click Me</Button>);
  const buttonElement = screen.getByText(/Click Me/i);
  expect(buttonElement).toBeInTheDocument();
});

Build Directory: Understanding the Production Output

When you build your React project for deployment, the output is placed in the build directory. This folder contains optimized, minified files that are ready to be served to users. For example:

build/
β”œβ”€β”€ index.html
β”œβ”€β”€ static/
β”‚   β”œβ”€β”€ css/
β”‚   β”œβ”€β”€ js/

The build directory is generated by running the npm run build command. This process bundles your code, removes unused code, and optimizes assets for performance. It’s essential to understand that this directory is meant for production and should not be edited manually.

Creating Custom Directories for Organization

While React provides a basic structure, you can create custom directories to better organize your project. For example:

  • hooks/: Store custom React hooks.
  • contexts/: Manage global state using React Context API.
  • services/: Handle API calls and business logic.

By creating these directories, you can separate concerns and keep your codebase modular.

Summary

In this article, we explored the core directories in a React project, diving into their roles and best practices. From the essential node_modules folder to the customizable src directory, each part of the React project structure plays an integral role in building scalable applications. We also discussed how to use configuration files like package.json and .env, organize tests, and understand the build directory.

By mastering React's project structure, you set the foundation for success in your development journey. An organized codebase not only improves productivity but also makes it easier to onboard new developers. For more detailed information, refer to the official React documentation. Remember, a well-structured project is the first step toward creating reliable, maintainable, and high-quality React applications.

Last Update: 24 Jan, 2025

Topics:
React