Sling Academy
Home/Next.js/Next.js: How to organize code in an “src” folder

Next.js: How to organize code in an “src” folder

Last updated: March 27, 2023

By default, Next.js puts your pages folder in the root directory of your project. However, you can store your source code (including pages, components, CSS styles, and utility functions) in an src folder (the name src is a convention and is strongly recommended, but isn’t mandatory). This will help you separate your source code from other files like configuration, public assets, tests, etc. Your projects (especially big and complex projects) will be organized in a logical way and making it easier to find and maintain your files in the far future.

To move your pages to the src folder, you need to follow the steps listed below:

  1. Create an src folder in the root directory
  2. Move your pages folder to the src folder.
  3. Move your styles folder to the src folder.
  4. Delete the .next folder to avoid caching-related problems (this folder will be auto-generated later).
  5. Restart your development server and enjoy your new folder structure.

It’s important to note that src/pages will be ignored if the pages folder is still present in the root directory.

In the src folder, you can add some subfolders, such as:

  • components: This folder contains components that can be used on multiple pages.
  • utils: This folder contains helper functions.

The file structure of the src folder:

.
├── components
├── pages
│   ├── _app.js
│   ├── _document.js
│   ├── api
│   │   └── hello.js
│   └── index.js
├── styles
│   ├── Home.module.css
│   └── globals.css
└── utils

If you are using Next.js with TailwindCSS, you also need to change the content property in your tailwind.config.js file to point to the new location of your pages. The update job is also necessary if you use other third-party libraries that need to configure paths.

The tutorial ends here. Happy coding & have a nice day!

Next Article: How to Add Google Analytics to Your Next.js App

Series: Next.js: Configuration & Deployment

Next.js

Related Articles

You May Also Like

  • Solving Next.js Image Component Error with CSS Classes
  • How to Use MUI (Material UI) in Next.js 14
  • Fixing Data Update Issues in Next.js Production
  • Fixing Next.js Undefined ENV Variables in Production Build
  • Solving Next.js SyntaxError: Unexpected token ‘export’
  • Next.js Error: Found Page Without a React Component as Default Export – How to Fix
  • Fixing Next.js Error: Blank Page on Production Build
  • Fixing Next.js Error when Importing SVGs: A Step-by-Step Guide
  • Next.js Issue: useEffect Hook Running Twice in Client
  • Fixing ‘self’ is not defined error in Next.js when importing client-side libraries
  • How to Dockerize a Next.js App for Production
  • How to set up Next.js with Docker Composer and Nginx
  • Solving Next.js CORS Issues: A Comprehensive Guide
  • Fixing Next.js Hydration Mismatch Error in Simple Ways
  • Next.js Error: Cannot use import statement outside a module
  • Next.js: How to Access Files in the Public Folder
  • Fixing Next.js Import CSS Error: A Simple Guide
  • Fixing LocalStorage Not Defined Error in Next.js
  • Fixing Next.js Error: No HTTP Methods Exported