-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to Organize a Next.js Full Stack Project #1
Comments
Now you can create next app with typescript enabled by default via: yarn create next-app next-full-stack --typescript |
Hi @Maxvien! This proposal is definitely a great approach to organising projects in Next.js (enforce cohesion, good discoverability and good understandability of the project). Personally, I like to follow your structure but adding an additional level of grouping within the main categories:
|
Hey @Maxvien, my project was becoming so convoluted and it didn't feel sustainable. Thanks for posting this cause it brought a LOT more clarity to my project. The |
@Maxvien I like your approach better |
Any One can share for new Next JS 14, App Router |
Hi there! In this article, I want to show you how I organize my Next.js Full Stack Project.
Folder Structure
First of all, you can take a look at the folder structure below. You can see that src is the root-level application project. Yes! I want to place my code into the src folder.
Shared Folder
I often have some code that I want to use for both frontend and backend, such as
schemas
for typed-safe API,services
for accessing environment variables or utility functions, ...With these kinds of code, I want to place them into the shared folder.
Backend Folder
In the backend folder, I want to place my server code, such as:
Frontend Folder
In the frontend folder, I want to place my client code, such as:
Pages Folder
pages is the default Next's folder for routing. Inside the pages folder, I have:
Setup Next.js
In this section, I will set up a Next.js application with the folder structure above.
Generate Next.js application
First, I will create a new Next.js application named next-full-stack with the command below.
Next, I change the working folder to next-full-stack.
Next, I remove these Next.js default folders.
Next, I create a new folder named src to store all my code.
Finally, I change the working folder to src to ready for the next steps.
Create Folders
To organize the folder structure which we have, I will run these command to create the folders.
Create Main Folders
Create Shared's Sub Folders
Create Backend's Sub Folders
Create Frontend's Sub Folders
Create Home Page
To create the home page for my project, I'll create a text file named
index.tsx
in the pages folder. The file will have the content below.Add TypeScript Support
I'd like to work with TypeScript on this project. So I will do these steps to have TypeScript support.
Run this command to let Next.js create
next-env.d.ts
andtsconfig.json
files for TypeScript configuration.Configure module aliases
Using relative import like this
import mod from '../../../mod.ts'
is so ugly. So I will add these lines totsconfig.json
, sectioncompilerOptions
to make my import code more beautiful.And my new
tsconfig.json
will look like this.From now on, I can import my child modules with these lines:
Or
Or
Demo: https://next-full-stack-git-issue-1.maxvien.vercel.app/
Source Code
You can find the source code of this tutorial in this branch: https://github.com/maxvien/next-full-stack/tree/issue-1
The text was updated successfully, but these errors were encountered: