Next.js has become one of the most popular React frameworks, empowering developers to build fast, scalable, and SEO-friendly applications. With its server-side rendering (SSR), static site generation (SSG), and API routes, Next.js provides a powerful toolkit for building modern web applications. Whether you are just starting with Next.js or want to learn how to set up and configure it for your next project, this guide will take you from zero to hero in no time.
In this step-by-step guide, we’ll cover everything you need to know to get started with Next.js, from installation and configuration to building your first page and deploying it. By the end of this article, you'll have a fully configured Next.js project ready for development.
What is Next.js?
Next.js is an open-source React framework developed by Vercel that streamlines building production-grade web applications.
Next.js is designed to improve performance, SEO, and developer experience by offering:
Why Choose Next.js for Your Project?
Here are some of the key reasons why you should consider Next.js for your next project:
Step 1: Installing Next.js
Before creating a Next.js project, ensure Node.js is installed on your machine. Check your version with:
node -vIf Node.js is installed, you will see the version number. Once ready, initialize your Next.js application using create-next-app:
npx create-next-app@latestFollow the interactive prompts to name your project and select your preferred configuration (TypeScript, ESLint, Tailwind CSS).
Alternatively, if using Yarn:
yarn create next-app my-next-appStep 2: Understanding the Next.js Project Structure
After the installation, you will have a folder named my-next-app (or whatever name you chose for your project). Inside this folder, you will find the standard Next.js directory structure:

my-next-app/
├── node_modules/
├── pages/
│ ├── _app.js
│ ├── index.js
├── public/
├── styles/
│ ├── globals.css
│ └── Home.module.css
├── .gitignore
├── package.json
└── README.mdHere’s a breakdown of the key files and directories:
Step 3: Running the Development Server
Now that you’ve set up your project, it's time to run the development server. Navigate to the project directory:
cd my-next-app
npm run devor, if you're using Yarn: yarn dev
The development server will start, and you can access your application by navigating to http://localhost:3000 in your browser.
Step 4: Creating Your First Page
Next.js uses file-based routing where each file inside the pages directory corresponds to a route.
Create a new file called pages/about.js:
// pages/about.js
export default function About() {
return (
<div>
<h1>About Us</h1>
<p>This is the About page for our Next.js app.</p>
</div>
);
}Visit http://localhost:3000/about in your browser to view your new page.
Step 5: Styling Your Next.js App
Next.js provides out-of-the-box support for global CSS and CSS Modules.
Add styles to styles/Home.module.css:
/* styles/Home.module.css */
.container {
margin: 0 auto;
padding: 50px;
max-width: 800px;
text-align: center;
}
h1 {
color: #0070f3;
}Import and apply the module styles in pages/index.js:
// pages/index.js
import styles from '../styles/Home.module.css';
export default function Home() {
return (
<div className={styles.container}>
<h1>Welcome to My Next.js App!</h1>
<p>This is the home page.</p>
</div>
);
}Refresh your browser to see the applied styles.
Step 6: Using API Routes
Next.js allows you to create serverless API endpoints directly inside your application.
Create an API handler file at pages/api/hello.js:
// pages/api/hello.js
export default function handler(req, res) {
res.status(200).json({ message: 'Hello from Next.js API!' });
}Visit http://localhost:3000/api/hello to receive the JSON response:
{ "message": "Hello from Next.js API!" }This feature allows you to build full-stack applications with serverless backend logic.
Step 7: Configuring Custom Routes
Next.js supports dynamic routing to handle parameters like blog slugs or user IDs.
Create a dynamic route file at pages/blog/[slug].js:
// pages/blog/[slug].js
import { useRouter } from 'next/router';
export default function BlogPost() {
const router = useRouter();
const { slug } = router.query;
return (
<div>
<h1>Blog Post: {slug}</h1>
<p>This is the content for the blog post titled {slug}.</p>
</div>
);
}Navigating to http://localhost:3000/blog/my-first-post will render the dynamic route with the corresponding query parameter.
Step 8: Deploying Your Next.js App
Next.js is designed for seamless deployment, especially on Vercel:
Congratulations! You've gone from zero to hero with Next.js, mastering project setup, routing, styling, API development, and deployment.



