Setting up a new Next.js project¶
Using npx create-next-app@latest¶
To create a new Next.js project, open your terminal or command prompt and run the following command:
npx create-next-app@latest
Let's break down this command:
npx: This is a tool that comes withnpm(Node Package Manager) and allows you to run Node.js package executables directly without installing them globally on your machine. This ensures you're always using the latest version ofcreate-next-app.create-next-app: This is the official CLI tool provided by Next.js for scaffolding new projects.@latest: This ensures that you get the most up-to-date version ofcreate-next-app, which is crucial for using the latest features like the App Router.
When you run this command, create-next-app will prompt you with a series of questions to configure your new project. You can typically press Enter to accept the default options for most of them, especially for a beginner course. Here's a common sequence of prompts you might see:
? What is your project named? › my-task-app
? Would you like to use TypeScript? › No / Yes (Choose Yes for better type safety)
? Would you like to use ESLint? › No / Yes (Choose Yes for code quality)
? Would you like to use Tailwind CSS? › No / Yes (Choose Yes, as we'll use it extensively)
? Would you like to use `src/` directory? › No / Yes
? Would you like to use App Router? (recommended) › No / Yes (Choose Yes, this is key for modern Next.js)
? Would you like to customize the default import alias (@/*)? › No / Yes
For this course, we highly recommend choosing Yes for TypeScript, ESLint, Tailwind CSS, and especially the App Router, as it's the latest and recommended way to build Next.js applications. Once you've answered the questions, create-next-app will install all the necessary dependencies and set up your project.
After the installation is complete, navigate into your new project directory:
cd my-task-app
You can now start the development server:
npm run dev
# or
yarn dev
This will start the Next.js development server, usually on http://localhost:3000. Open your browser and navigate to this URL, and you should see the default Next.js starter page! Congratulations, you've created your first Next.js application!
Understanding the Initial Project Structure¶
Now that you have a Next.js project up and running, let's explore its initial file structure. Understanding these core directories and files is fundamental to building applications with Next.js, especially when using the App Router.
Here's a simplified overview of what you'll find inside your my-task-app directory (the exact structure might vary slightly based on your create-next-app choices, like src/ directory usage, but the core elements remain):
my-task-app/
├── node_modules/
├── public/
│ └── next.svg
│ └── vercel.svg
├── app/
│ ├── layout.js (or .tsx)
│ ├── page.js (or .tsx)
│ └── favicon.ico
├── next.config.js
├── package.json
├── package-lock.json (or yarn.lock)
├── postcss.config.js
├── tailwind.config.js
├── README.md
└── .git/ (if initialized)
Let's dive into the most important parts:
-
app/Directory (The Heart of Your Application with App Router) This directory is the cornerstone of your Next.js application when using the App Router. Next.js uses a file-system based routing mechanism, meaning that folders withinapp/define your application's routes.layout.js(or.tsx): This file defines the UI that is shared across multiple routes. It wraps around yourpage.jsfiles and any nestedlayout.jsfiles. Think of it as a template that applies to a segment of your application. For example, your mainlayout.jsat the root ofapp/might contain your navigation bar, footer, and apply global styles (like Tailwind's base styles).page.js(or.tsx): This file is responsible for rendering the UI of a specific route segment and making it publicly accessible. If a folder inapp/contains apage.jsfile, that folder becomes a routable segment. For example,app/page.jscorresponds to the root route (/).favicon.ico: The icon displayed in the browser tab.
Key Concept: In the App Router, files like
layout.jsandpage.jsare Server Components by default. This means they are rendered on the server, which brings performance benefits like faster initial page loads and improved SEO. We'll explore Server and Client Components in more detail later. -
public/Directory This directory is used for serving static assets. Any file placed insidepublic/will be served directly from the root of your application.- Examples: Images, fonts, favicons, or other static files that don't need to be processed by Next.js's build system.
- Usage: If you have an image
public/my-image.png, you can access it in your code using/my-image.png. - For our Task App, this is where we might place a custom favicon or any background images.
-
next.config.jsThis file is a powerful configuration file for your Next.js project. It allows you to customize various aspects of Next.js's behavior, such as:- Environment Variables: Setting up environment variables for different deployment environments (development, production).
- Image Optimization: Configuring domains for
next/image. - Redirects, Rewrites, Headers: Customizing routing and HTTP headers.
- Webpack Configuration: Extending Webpack for advanced build needs.
- Output Directory: Changing the build output directory.
Initially, this file will be quite minimal. As your Task App grows, you might find yourself adding configurations here.
-
package.jsonThis file defines your project's metadata, scripts, and dependencies.scripts: Contains shortcuts for common commands (e.g.,devfor starting the development server,buildfor creating a production build,startfor running the production build).dependencies: Lists all the third-party packages your application needs to run (e.g.,react,react-dom,next).devDependencies: Lists packages needed only for development (e.g.,eslint,tailwindcss).
node_modules/This directory contains all the installed packages and their dependencies listed inpackage.json. You should never manually edit files in this directory or commit it to version control (it's typically ignored by Git using.gitignore).- Tailwind CSS Configuration Files
If you opted for Tailwind CSS during setup, you'll see:
tailwind.config.js: This file is where you customize your Tailwind CSS theme, extend colors, add custom utility classes, and configurecontentto specify which files Tailwind should scan for classes.postcss.config.js: PostCSS is a tool for transforming CSS with JavaScript. Tailwind CSS uses PostCSS plugins to compile its utility classes.
This initial structure provides a robust foundation for building modern web applications with Next.js. In the next section, we'll start modifying these files to build the basic layout for our Task Management App.