Veröffentlicht · wird verbessert
Next.js-Anleitung · 1/6
Dieses Kapitel ist vorerst nur auf Englisch verfügbar.
In this chapter you will create a Next.js project, start the development server, edit your first page and add a second one. We will also set up the editor so it catches mistakes early. This guide uses the App Router, which lives in the app directory. The older pages directory (the Pages Router) is still supported, but the App Router is the recommended choice for new projects.
Next.js runs on Node.js. Installing the current LTS release from nodejs.org is the safest choice. Each Next.js release declares a minimum Node.js version, so if your Node.js is old, check the requirements in the official installation guide. You can use npm, which ships with Node.js, or pnpm, Yarn or Bun.
node --version
npm --versionThe quickest way to start is the official scaffolding tool, create-next-app. It installs the required packages and writes sensible configuration files.
npx create-next-app@latest my-app
# or with another package manager
pnpm create next-app my-app
yarn create next-app my-app
bun create next-app my-appThe tool asks a few questions. Accepting the recommended defaults gives you TypeScript, a linter (ESLint), Tailwind CSS, the App Router and the @/* import alias. If you customize, you will be asked about the following; the exact prompts can vary between releases.
src/ directory@/* by default)If you are new to Next.js, stick with the defaults. You can add TypeScript later, but enabling it from the start means your editor checks component props and data shapes for you.
Move into the project folder and start the dev server:
cd my-app
npm run devOpen http://localhost:3000 and you will see the starter page. The dev server rebuilds only what changed whenever you save, and Fast Refresh updates the page while preserving component state where it can. Recent releases use Turbopack, a bundler written in Rust, for the dev server by default.
Your package.json contains scripts like these:
{
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "eslint"
}
}dev starts the development server, creates a production build, and serves that build. To use another port, pass , for example .
buildstart-pnpm run dev -- -p 4000In the App Router, every page.tsx file inside app becomes a URL. app/page.tsx is the home page (/). Replace its content with the following and save; the browser updates immediately.
// app/page.tsx
export default function Page() {
return (
<main>
<h1>Hello, Next.js!</h1>
<p>I just edited my first page.</p>
</main>
);
}The shared frame around every page is app/layout.tsx. This root layout is required and must contain the <html> and <body> tags. Set the lang attribute to your content language so search engines and screen readers handle it correctly.
Create a folder with a page.tsx inside it and you get a new route: app/about/page.tsx becomes /about. To move between pages, use the Link component from next/link instead of a plain <a>. Link performs client-side navigation without a full reload and prefetches the destination of links that are visible on screen, so navigation feels instant.
// app/about/page.tsx
import Link from "next/link";
export default function AboutPage() {
return (
<main>
<h1>About</h1>
<Link href="/">Back home</Link>
</main>
);
}If you use VS Code, these steps are worth a few minutes:
.ts or .tsx file open, run "TypeScript: Select TypeScript Version" from the command palette and choose "Use Workspace Version". The editor will then flag invalid config values and misuse of "use client".npx create-next-app@latest and start it with npm run dev.page.tsx under app is a route, and layout.tsx provides the shared frame.Link from next/link.
0 Kommentare
Anmelden · Melde dich an, um einen Kommentar zu schreiben.
Schreib den ersten Kommentar.