Next.js Getting Started
Set up a Next.js project, add pages and layouts, and run the dev server in minutes.
TL;DR
- Create a project with
npx create-next-app@latestand accept the defaults. - Add a route by creating a
page.tsxfile insideapp. - Run
npm run devand openlocalhost:3000to see changes.
Create a Project
create-next-appScaffold a new app with TypeScript, Tailwind, and the App Router ready to go.
npx create-next-app@latest my-app
cd my-appnpm run devStart the local dev server with Turbopack and instant fast refresh.
npm run dev
# open http://localhost:3000npm run buildCreate the production build, then serve it with the built-in Node.js server.
npm run build
npm run startPages and Layouts
app/page.tsxExport a default component from this file to render the home page.
// app/page.tsx
export default function Page() {
return <h1>Hello, Next.js</h1>;
}app/about/page.tsxAdd a folder with a page.tsx file to create a route at that path.
// app/about/page.tsx -> /about
export default function About() {
return <h1>About us</h1>;
}app/layout.tsxWrap every page in shared UI; the root layout renders the html and body tags.
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}Links and Interactivity
<Link>Move between pages without a full page reload by using the Link component.
import Link from 'next/link';
export default function Nav() {
return <Link href="/about">About</Link>;
}'use client'Add this line to a file when the component needs state or event handlers.
'use client';
import { useState } from 'react';
export default function Counter() {
const [n, setN] = useState(0);
return (
<button onClick={() => setN(n + 1)}>
Clicked {n}
</button>
);
}async PageFetch data right inside a page, because pages are Server Components by default.
const API = 'https://api.example.com';
export default async function Page() {
const res = await fetch(`${API}/posts`);
const posts = await res.json();
return <p>{posts.length} posts</p>;
}API Routes and Config
route.tsExport a function named after an HTTP method to create an API endpoint.
// app/api/hello/route.ts
export async function GET() {
return Response.json({ message: 'Hello' });
}next.config.tsSet project-wide options in this file at the root of the project.
import type { NextConfig } from 'next';
const nextConfig: NextConfig = {
poweredByHeader: false,
};
export default nextConfig;.env.localStore secrets here; only names starting with NEXT_PUBLIC_ reach the browser.
# .env.local
DATABASE_URL="postgres://..."
NEXT_PUBLIC_SITE_NAME="My App"Tips
- Accept the
create-next-appdefaults for TypeScript, Tailwind, and the App Router unless your team already uses something else. - Upgrade
next,react, andreact-domtogether, because each Next.js release expects a matching version of React.
Warnings
- Next.js 16 needs Node.js 20.9 or newer, so
npm run devfails on older versions of Node. - Hooks like
useStateonly work in files marked'use client', because pages are Server Components by default.
In Practice
Build a home page that fetches posts on the server and links to each one.
- The page is an async Server Component, so it can await data directly.
- The fetch runs on the server, so the browser never sees the API call.
- Each post becomes a Link, which gives fast client-side navigation.
- No state or effects are needed, because the HTML arrives already filled in.
// app/page.tsx
import Link from 'next/link';
type Post = { id: number; title: string };
const API = 'https://jsonplaceholder.typicode.com';
export default async function Page() {
const res = await fetch(`${API}/posts?_limit=5`);
const posts: Post[] = await res.json();
return (
<ul>
{posts.map(({ id, title }) => (
<li key={id}>
<Link href={`/posts/${id}`}>{title}</Link>
</li>
))}
</ul>
);
}FAQ
Next.js is a React framework for building full websites and apps. It adds routing, server rendering, data fetching, and API endpoints on top of React. You write components, and Next.js handles the build and the server.
Yes, a little. Learn components, props, and useState first. Next.js adds routing and server features around those same ideas.
The App Router uses the app folder and Server Components. The Pages Router uses the pages folder and is the older system. New projects should use the App Router.
Next.js 16 requires Node.js 20.9 or newer. Check your version by running node -v in a terminal.