Course map
Stage 1: App Router Foundations
Stage 2: The Rendering Model
Stage 3: Data, Caching and Mutations
Stage 4: Full-Stack Next.js
Stage 5: Production and Specialization

Part 01 / Stage 1

Project setup and the App Router mental model

Goal: Scaffold a real Next.js 16 project with create-next-app, understand exactly what next dev, next build and next start each do, read the generated tsconfig.json and next.config.ts instead of guessing at them, and place the App Router against the (no-longer-taught) Pages Router in one paragraph. Time: ~4 h · Level: 🟢 comfortable with React (see the roadmap's prerequisites), new to Next.js

What you will learn

  • What Next.js adds on top of React, and the App Router vs. Pages Router split
  • Installing Node.js 22 and npm, and Next.js's own minimum version requirements
  • Scaffolding with create-next-app and what every flag actually does
  • Touring a generated project: app/, public/, config files, and the files create-next-app writes that you never asked for
  • next dev, next build, next start — three different commands, three different jobs
  • Reading a next build route table: ○ (Static) today, ƒ (Dynamic) and ◐ (Partial Prerender) from Stage 2 onward
  • next.config.ts and tsconfig.json, read line by line against the real file this course's harness generated
  • Why this course's examples run inside one shared app (harness/) instead of a new project per lesson

Topics in this part

Practice

Every exercise edits harness/app/page.tsx (or next.config.ts where stated), runs a real command against it, and restores the file to the README's section 7 welcome banner afterward — the same file the next part starts from. Copy the exact commands you ran and their real output into your own notes; do not type output from memory.


Exercise 1: My intro card

Scenario: On day one of this course, print a small intro card as your homepage, the same way the Java course's Part 1 opens with one. What to do: Replace app/page.tsx with a component that renders a <pre> block: a line of =, your name/city/goal on three lines, a matching line of = below.

Sample output (curl -s http://localhost:PORT/ | grep -o '<pre>.*</pre>', port varies):

<pre>==============================
Name : Priya Sharma
City : Bengaluru
Goal : Ship a real Next.js app
==============================</pre>

💡 Hint: This is plain JSX inside a template string, exactly like examples/03-welcome-banner-page.tsx in the README — no routing, no new file, just a different string.


Exercise 2: Fix the broken homepage

Scenario: A friend sends you this app/page.tsx. It does not build.

function Home() {
  return (
    <main>
      <h1>Good morning</h1>
      <p>Have a nice day
    </main>
  );
}

What to do: Fix every error so npm run build succeeds, keeping the same three lines of text (add a closing </p> and Bye as a third line).

What actually happens, one bug at a time (both reproduced for real against this course's harness):

Bug 1 — the unclosed <p> tag fails to even parse:

$ npm run build
Error: Turbopack build failed with 1 error:
./app/page.tsx:8:1
Error: Unexpected token. Did you mean `{'}'}` or `&rbrace;`?
  6 |     </main>
  7 |   );
> 8 | }
  9 |

Bug 2 — once the JSX is valid, forgetting export default fails type checking instead, caught by the generated route types (README section 8):

$ npm run build
✓ Compiled successfully in 216ms
  Running TypeScript ...
app/page.tsx(49,31): error TS2344: Type 'typeof import(".../app/page")' does not satisfy the constraint 'AppPageConfig<"/">'.
Failed to type check.

💡 Hint: Fix the JSX first (an unescaped } or an unclosed tag is always a parse error, before TypeScript ever runs), rebuild, then fix the type error. Two different failure stages, two different error shapes — that pairing itself is the lesson.


Exercise 3: Extend the environment check

Scenario: examples/02-env-check-page.tsx printed Node's version and platform. A teammate asks for two more facts: the OS architecture and the current working directory. What to do: Import os from node:os, and add two <li> lines using os.arch() and process.cwd().

Sample output (ran for real against this course's harness; your cwd path will differ):

<ul><li>Node.js: v22.23.2</li><li>Platform: darwin</li><li>Arch: arm64</li><li>Cwd: /Users/sujeet.s/Documents/RND/learning/nextjs-step-by-step/harness</li></ul>

💡 Hint: import os from "node:os"; then os.arch(). This still runs on the server only — there is no browser equivalent of process.cwd(), which is itself evidence that this file's code never ships to the client (Part 6 explains exactly why).


Exercise 4: Build into a different folder

Scenario: You want next build's output in a folder named build/ instead of the default .next/, the same instinct as javac -d in the Java course. What to do: Add distDir: "build" to next.config.ts, run next build, confirm the new folder exists, then remove it and revert the config.

Sample output (run for real):

$ npm run build
...
Route (app)
┌ ○ /
└ ○ /_not-found
$ ls -d build .next
.next
build
$ rm -rf build

💡 Hint: .next/ from the previous build is not deleted automatically when you change distDir — both folders exist side by side until you clean up by hand. Revert next.config.ts afterward; later parts assume the default .next/.


Exercise 5: Catch a type error at build time

Scenario: tsconfig.json's "strict": true (README section 8) is supposed to catch real bugs, not just style nits. Prove it does. What to do: In app/page.tsx, declare const title: string = 42; and render {title}. Run next build, copy the exact error, then remove the line.

Sample output (run for real):

$ npm run build
✓ Compiled successfully in 296ms
  Running TypeScript ...
app/page.tsx(2,9): error TS2322: Type 'number' is not assignable to type 'string'.
Failed to type check.

💡 Hint: This is the same tsc strict-mode check any TypeScript project has — Next.js just happens to run it as a required step inside next build, so a type bug never reaches next start.


Exercise 6: Trigger Cache Components' own guardrail

Scenario: next.config.ts has cacheComponents: true (README section 8) — nothing is cached silently. Prove it also refuses to silently freeze a value that keeps changing. What to do: Set app/page.tsx to return <main>Built at: {new Date().toISOString()}</main>; and run next build. Copy the real error. Then remove the line and confirm the build is green again.

Sample output (run for real against this course's harness):

$ npm run build
  Generating static pages using 5 workers (1/4) 
Error: Route "/": Next.js encountered the unstable value `new Date()` while prerendering.

This value can change between renders, so it must be either prerendered or computed later.

Ways to fix this:
  - [dynamic] Render at request time by adding a dynamic data access (e.g. `await connection()`) before this call
  - [cache] Prerender and cache the value with `"use cache"`
  - [client] Render the value on the client with `"use client"`
  - [measure] If the value is for telemetry, use a timing API such as `performance.now()`

This course does not ask you to apply any of those four fixes yet — "use cache" is Stage 3's own topic, "use client" is Part 6's, and connection() needs a <Suspense> boundary Part 7 teaches. Removing the line (what examples/03-welcome-banner-page.tsx actually does — it reads process.version, not the clock) is the correct Part 1 fix. 💡 Hint: process.version worked fine earlier in this part with no such error — it is deterministic for the whole life of one build, while new Date() is not. Cache Components only objects to values that could legitimately differ between two prerenders of the same page.


Exercise 7: Read the generated route types, then remove the only route

Scenario: Section 8 mentioned .next/types/routes.d.ts as the file behind LayoutProps<"/">. See it change with your own eyes. What to do:

  1. Open harness/.next/types/routes.d.ts and copy the line starting type AppRoutes =.
  2. Temporarily move app/page.tsx out of the way (mv app/page.tsx /tmp/page-aside.tsx) and run next build.
  3. Open routes.d.ts again and copy the same line.
  4. Move the file back (mv /tmp/page-aside.tsx app/page.tsx) and rebuild to confirm you're back to a green build.

Sample output (run for real):

$ grep AppRoutes harness/.next/types/routes.d.ts
type AppRoutes = "/"

$ mv app/page.tsx /tmp/page-aside.tsx && npm run build
app/dev/types/validator.ts(48,39): error TS2307: Cannot find module '../../../app/page.js' or its corresponding type declarations.
Failed to type check.

$ grep AppRoutes harness/.next/types/routes.d.ts
type AppRoutes = never

$ mv /tmp/page-aside.tsx app/page.tsx && npm run build
✓ Generating static pages using 5 workers (4/4)

💡 Hint: The build itself fails once there is no home route at all (step 2) — route types are still regenerated as the very first thing that happens, before the failing type-check step runs, which is why AppRoutes already shows never even though the overall build did not finish cleanly. routes.d.ts is Next.js's javap: a generated file nobody hand-writes, that exists purely so tools (and you) can see what the framework actually resolved.

Show the solutions for Part 1 (7 files)

solutions/ex01.tsx

// Solution to Exercise 1. Drop in as harness/app/page.tsx, then:
//   curl -s http://localhost:PORT/ | grep -o '<pre>.*</pre>'
export default function Home() {
  return (
    <main>
      <pre>
        {`==============================
Name : Priya Sharma
City : Bengaluru
Goal : Ship a real Next.js app
==============================`}
      </pre>
    </main>
  );
}

solutions/ex02.tsx

// Solution to Exercise 2. The two bugs fixed:
//   1. the unclosed <p> tag (a parse error, caught before TypeScript runs)
//   2. the missing `export default` (a type error, caught by the
//      generated route types — see README section 8)
export default function Home() {
  return (
    <main>
      <h1>Good morning</h1>
      <p>Have a nice day</p>
      <p>Bye</p>
    </main>
  );
}

solutions/ex03.tsx

// Solution to Exercise 3. Drop in as harness/app/page.tsx.
import os from "node:os";
import packageJson from "../package.json";

export default function Home() {
  return (
    <main>
      <h1>Environment check</h1>
      <ul>
        <li>Node.js: {process.version}</li>
        <li>Platform: {process.platform}</li>
        <li>Arch: {os.arch()}</li>
        <li>Cwd: {process.cwd()}</li>
        <li>next (from package.json): {packageJson.dependencies.next}</li>
      </ul>
    </main>
  );
}

solutions/ex04.md

# Solution to Exercise 4: build into a different folder

Add `distDir` to `harness/next.config.ts`:
```ts
import type { NextConfig } from "next";

const nextConfig: NextConfig = {
  cacheComponents: true,
  partialPrefetching: true,
  distDir: "build",
  turbopack: {
    rules: {
      "*.css": {
        loaders: ["@tailwindcss/turbopack"],
        as: "*.css",
      },
    },
  },
};

export default nextConfig;
```

Run, confirm, and clean up — exact transcript from running this against the course harness:
```text
$ npm run build
Route (app)
┌ ○ /
└ ○ /_not-found

○  (Static)  prerendered as static content
$ ls -d build .next
.next
build
$ rm -rf build
```

Revert `next.config.ts` to drop the `distDir: "build"` line before moving on — Part 2 onward assumes the default `.next/` output folder.

solutions/ex05.tsx

// Solution to Exercise 5. The broken version to reproduce the error:
//
//   export default function Home() {
//     const title: string = 42;
//     return <main>{title}</main>;
//   }
//
// Real error from `npm run build` against that version:
//   app/page.tsx(2,9): error TS2322: Type 'number' is not assignable to type 'string'.
//   Failed to type check.
//
// Fixed version (remove the bad assignment, keep the rest of the course
// harness's actual homepage):
export default function Home() {
  return (
    <main style={{ fontFamily: "system-ui", padding: "2rem", maxWidth: 560 }}>
      <pre>
        {`========================================
   Welcome to Next.js Step by Step!
   Learner: Priya, Bengaluru
========================================`}
      </pre>
      <p>Today&apos;s plan: create-next-app -&gt; next dev -&gt; next build</p>
      <ul>
        <li>Node.js: {process.version}</li>
        <li>Rendered by: app/page.tsx</li>
      </ul>
    </main>
  );
}

solutions/ex06.tsx

// Solution to Exercise 6. The version that triggers Cache Components'
// guardrail (do NOT leave this in harness/app/page.tsx):
//
//   export default function Home() {
//     return <main>Built at: {new Date().toISOString()}</main>;
//   }
//
// Real error from `npm run build` against that version:
//   Error: Route "/": Next.js encountered the unstable value `new Date()`
//   while prerendering. This value can change between renders, so it
//   must be either prerendered or computed later.
//
// Part 1's correct fix is simply not to read the clock in a page this
// course hasn't taught how to make dynamic yet — restore the harness's
// real homepage instead:
export default function Home() {
  return (
    <main style={{ fontFamily: "system-ui", padding: "2rem", maxWidth: 560 }}>
      <pre>
        {`========================================
   Welcome to Next.js Step by Step!
   Learner: Priya, Bengaluru
========================================`}
      </pre>
      <p>Today&apos;s plan: create-next-app -&gt; next dev -&gt; next build</p>
      <ul>
        <li>Node.js: {process.version}</li>
        <li>Rendered by: app/page.tsx</li>
      </ul>
    </main>
  );
}

solutions/ex07.md

# Solution to Exercise 7: read the generated route types

Exact transcript from running this against the course harness:
```text
$ grep AppRoutes harness/.next/types/routes.d.ts
type AppRoutes = "/"

$ mv harness/app/page.tsx /tmp/page-aside.tsx
$ (cd harness && npm run build)
...
.next/dev/types/validator.ts(48,39): error TS2307: Cannot find module '../../../app/page.js' or its corresponding type declarations.
Failed to type check.

$ grep AppRoutes harness/.next/types/routes.d.ts
type AppRoutes = never

$ mv /tmp/page-aside.tsx harness/app/page.tsx
$ (cd harness && npm run build)
...
Route (app)
┌ ○ /
└ ○ /_not-found

○  (Static)  prerendered as static content
```

`AppRoutes` flips from `"/"` to `never` the moment the only page in the project is gone, and back once it returns — confirmed by reading the generated `.next/types/routes.d.ts` directly, not assumed. The overall build still fails with no home route (there is nothing to type-check against), but the route-types step runs before the failing type-check step, which is why the file update is visible even though the build as a whole did not finish cleanly.

This is NOT the Next.js you know

This version has breaking changes — APIs, conventions, and file structure may all differ from your training data. Read the relevant guide in node_modules/next/dist/docs/ ... before writing any code. Heed deprecation notices.

That block is Next.js's own acknowledgment, written into every new project, that it changes fast enough to outdate a training cutoff — exactly the risk this course manages by stating a verified version everywhere (section 3) instead of writing "generically."

It is also why every lesson in this course points at one shared app, `harness/` (the project you just toured), instead of a fresh `create-next-app` per part: a brand-new scaffold per lesson would mean re-verifying flags and versions dozens of times for no teaching benefit, and routes added in later parts (file-based routing, layouts, Server Actions, a database) need to coexist in one real app to show how they compose — the same reason this course's capstones (Recipe Box, QuickCart) are single apps, not one-off snippets.

## Common mistakes
| Mistake | Why it is wrong | Fix |
|---|---|---|
| `page.tsx` with no `export default` | Reproduced for real in this part: `next build` fails with `error TS2344: ... does not satisfy the constraint 'AppPageConfig<"/">'`. The generated route types (section 8) catch it, not a vague runtime error. | Add `export default` to the component function |
| Running `next start` before `next build` | `start` only **serves** a build; it does not create one | `next build` first, every time |
| Assuming `next dev`'s port is always 3000 | If port 3000 is busy, Next.js silently picks the next free one and tells you in the banner | Read the `- Local:` line in the dev server's own output, don't hardcode `3000` |
| Treating `○ Static` as "cached" | With `cacheComponents: true` (default), static just means "no dynamic data was found" — nothing was cached on purpose | Don't reach for caching vocabulary (Stage 3) until a page actually needs it |
| Editing `next-env.d.ts` by hand | It is regenerated on every `dev`/`build` run; hand edits are silently lost | Never edit it; it is not meant to be read either |
| Expecting `pages/` to still work for new code | This course's App Router and the legacy Pages Router are two different systems in the same framework | Build everything under `app/`, per section 1 |
| Deleting `AGENTS.md` to "clean up" | `next dev` writes it back on the next run — it is not a stray file | Leave it; it costs nothing and isn't shipped to users |

## Quick recap
- Next.js adds routing, a server runtime, and a build step on top of React; this course only ever teaches the App Router, never the Pages Router.
- `create-next-app --ts --app --tailwind --eslint --use-npm` scaffolds this course's whole toolchain in one command — version-check it with `node_modules/*/package.json`, never assume.
- `next dev` is for writing code, `next build` compiles and decides what can be pre-rendered, `next start` only serves what `build` already produced.
- A `next build` route table's `○`/`ƒ`/`◐` symbols describe when a route's HTML was produced — proven here by `process.version` freezing into `.next/server/app/index.html` at build time.
- `next.config.ts` and `tsconfig.json` are the two files worth reading on day one; `cacheComponents`/`partialPrefetching` are on by default in this version, confirmed from the installed package's own docs.
- `AGENTS.md` is Next.js's own warning that this framework moves fast — the reason this course states its verified version everywhere instead of writing timelessly.

## Checklist
- [ ] I installed Node.js 22+ and confirmed `node --version` / `npm --version`.
- [ ] I ran `create-next-app` myself (or read this part's exact transcript) and can explain every flag it was given.
- [ ] I can name what `app/layout.tsx`, `app/page.tsx`, `next.config.ts` and `tsconfig.json` are each for, without opening them again.
- [ ] I ran `dev`, `build` and `start` myself and can say in one sentence what each one does differently.
- [ ] I read a real `next build` route table and explained why `/` was `○`, not `ƒ` or `◐`.
- [ ] I edited `app/page.tsx` myself and saw the change over `curl`, not just in a browser I'm trusting blindly.
- [ ] I can state the App Router vs. Pages Router split in one paragraph, unprompted.

Search the guide