Skip to lesson content

PROJECT 1 · LESSON 1 OF 18

Create your first Next.js website

Run a small website and understand which file produces the home page.

  • App Router
  • page.tsx
  • root layout
  • development server

Your workspace: edushop

Start a new practice folder. The completed checkpoint includes all configuration and source files.

Extract the ZIP, open its project folder, and run npm install. Database lessons also need the .env.local setup described below. Checkpoints never contain credentials or node_modules.

1. Understand the problem and the concepts

Why we need this feature

You already know how to write a React component. The first problem is making that component available at a real URL. Next.js supplies the routing, rendering and build system around React. In this lesson, watch how a browser request reaches a page instead of treating the project as a collection of files to copy.

You will build EduShop in one folder through Projects 1–3, then return to it in Project 6. Projects 4–5 use a separate task-app folder. Keep both folders outside the EduPoly website repository.

Use Node.js 20.9 or newer and npm. These examples pin Next.js 16.3 and React 19.2. Keep Cache Components disabled for this introductory course. Basic JavaScript and React props/state are prerequisites.

What React does, and what Next.js adds

React describes a user interface as a tree of components. A component returns JSX: a description of elements such as headings and paragraphs. React alone does not decide that /about should display About or provide your application’s production server. Next.js adds these application-level conventions. You still write React components, but place certain components in specially named files.

The App Router uses the app directory to define routes. app/page.tsx is the page for /. The word page is significant; Home is simply the function name we chose. Renaming Home to Welcome keeps the URL unchanged. Renaming page.tsx to welcome.tsx removes its special routing role.

Follow the root layout and its children

The root layout supplies the HTML document around your page. Its html and body elements are required here because this is the top-level layout. children is the place where Next.js inserts the selected page. For /, children is the home-page output. Later, /about will use the same layout with a different child.

Our navigation lives outside main, while the page sits inside main. That means every page receives the same navigation without importing it separately. Do not paste a second html or body element into page.tsx: that file supplies the page content, not a second document.

Know which files you edit and which files tools generate

package.json declares dependencies and named commands. npm install reads it and installs packages into node_modules; npm run dev executes the dev command. package-lock.json records the resolved installation and should be retained in your own project. .next contains generated output and should not be edited.

TypeScript’s .tsx extension allows JSX with type checking. tsconfig.json configures that checker and the @/* import alias. The alias lets @/components/faq refer to components/faq from the project root. next-env.d.ts supplies framework types. These setup files support the application; the visible heading still comes from app/page.tsx.

For now, npm run dev starts a development server with change detection. npm run build prepares optimized production output, and npm run start serves that output. Changing a source file while using start does not rebuild it automatically.

2. Follow the execution flow

Read this sequence before the source. Identify the input, the component or function that handles it, and the result the learner sees.

  1. The browser requests http://localhost:3000/.

  2. Next.js selects app/page.tsx for the root route.

  3. The root layout places the selected page in its children slot.

  4. The server supplies the initial rendered document; the browser displays the heading and navigation.

Trace the first heading

  1. Find <h1>Welcome to EduShop</h1> in app/page.tsx. The text is a JSX child, not a database value.
  2. Change only Welcome to EduShop to My learning store. Save and observe the browser update.
  3. Now change the text of the Home link in app/layout.tsx. The navigation changes independently of the page heading.
  4. This separation is the first architectural boundary: the layout owns shared structure, and the page owns route-specific content.

3. Build it, one file at a time

  1. Download this lesson’s completed checkpoint and extract it. It includes package.json, TypeScript configuration and a small stylesheet. Alternatively create a blank folder and add every file shown below.
  2. Run npm install, then npm run dev inside that folder. Open http://localhost:3000.
  3. Change the heading and save. Explain the relationship between app/page.tsx and the / URL.
Terminal
npm install
npm run dev

The paths below are relative to your practice project. Each block is the complete file at this checkpoint. Read its responsibility first, type or copy the code, then use the walkthrough to explain the important statements.

package.json

This is the project’s dependency and command manifest. It tells npm what to install and how to run the learner application.

package.json
{
  "name": "edupoly-nextjs-lab",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "typecheck": "tsc --noEmit"
  },
  "dependencies": {
    "next": "16.3.0",
    "react": "19.2.8",
    "react-dom": "19.2.8"
  },
  "devDependencies": {
    "typescript": "^5",
    "@types/node": "^20",
    "@types/react": "^19",
    "@types/react-dom": "^19"
  }
}

Read the code

  1. dependencies lists packages needed by the application; devDependencies lists development tools and TypeScript definitions. Install from the folder containing this file.
  2. scripts.dev runs the development server; scripts.build prepares production output; scripts.start serves that output. typecheck checks TypeScript without emitting JavaScript.
  3. When a later lesson replaces this file to add database or auth packages, run npm install again. Keep your generated package-lock.json so subsequent installs use a consistent resolution.

tsconfig.json

This configures TypeScript and module resolution; it does not define a browser route.

tsconfig.json
{
  "compilerOptions": {
    "target": "ES2017",
    "lib": ["dom", "dom.iterable", "esnext"],
    "strict": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "bundler",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "react-jsx",
    "skipLibCheck": true,
    "plugins": [
      {
        "name": "next"
      }
    ],
    "paths": {
      "@/*": ["./*"]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
  "exclude": ["node_modules"]
}

Read the code

  1. strict enables stronger checks for missing values and mismatched types. noEmit lets Next.js own compilation while TypeScript checks correctness.
  2. The @/* paths mapping points imports at the project root. @/lib/products therefore resolves to lib/products, not a network address.
  3. include identifies files to check and exclude avoids checking installed dependency source. Next.js can add framework-required settings when it builds.

next-env.d.ts

This declaration file makes Next.js and image-import types available to TypeScript.

next-env.d.ts
/// <reference types="next" />
/// <reference types="next/image-types/global" />

Read the code

  1. The triple-slash references load framework type declarations, not executable browser code.
  2. Next.js manages this file during development. Put application types in your own modules instead of adding business logic here.

next.config.ts

This is the learner application’s framework configuration.

next.config.ts
export default {};

Read the code

  1. The empty export uses the framework defaults for this course. It deliberately does not enable Cache Components.
  2. Do not confuse this file with a page or an environment file. Changing framework configuration may require restarting the development server.

.gitignore

This keeps generated files and local secrets out of your practice repository.

.gitignore
node_modules/
.next/
.env*
!.env.example
*.tsbuildinfo

Read the code

  1. node_modules and .next can be regenerated from source and dependencies. They should not be committed.
  2. .env* excludes local environment files, while !.env.example allows a safe placeholder template. Ignoring a file does not remove a secret already committed to history.

app/globals.css

This shared stylesheet makes the practice application readable without introducing a styling framework.

app/globals.css
* {
  box-sizing: border-box;
}
body {
  margin: 0 auto;
  max-width: 1000px;
  padding: 24px;
  font: 17px/1.6 system-ui;
  color: #17243c;
}
nav {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  padding: 16px 0;
  border-bottom: 1px solid #ccd4e0;
}
a {
  color: #174b93;
}
main {
  padding: 24px 0;
}
article,
form {
  border: 1px solid #ccd4e0;
  padding: 18px;
  margin: 16px 0;
  border-radius: 10px;
}
label {
  display: block;
  margin: 8px 0;
}
input,
button,
select {
  font: inherit;
  padding: 8px;
  max-width: 100%;
}
button {
  cursor: pointer;
}
button:disabled {
  cursor: wait;
  opacity: 0.6;
}
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 16px;
}
:focus-visible {
  outline: 3px solid #d74819;
  outline-offset: 3px;
}

Read the code

  1. The layout imports this stylesheet once so its element and class rules apply throughout the app.
  2. The .grid rule adapts cards to the available width. The focus-visible rule gives keyboard users a visible focus indicator.
  3. These are ordinary CSS rules; none of them creates a route, fetches data or changes React state.

app/layout.tsx

This defines the document and shared structure surrounding the selected page.

app/layout.tsx
import type { ReactNode } from "react";
import Link from "next/link";
import "./globals.css";
export const metadata = {
  title: "EduShop",
  description: "My Next.js learning project",
};
export default function Layout({ children }: { children: ReactNode }) {
  return (
    <html lang="en">
      <body>
        <nav>
          <Link href="/">Home</Link>
          <Link href="/about">About</Link>
          <Link href="/contact">Contact</Link>
        </nav>
        <main>{children}</main>
      </body>
    </html>
  );
}

Read the code

  1. html and body belong to this root layout. children is the selected route content and must appear in the returned tree.
  2. Shared navigation belongs here so individual pages do not copy it. Link destinations are URL paths, not source filenames.
  3. The stylesheet import applies the shared presentation. The metadata export describes the document rather than rendering visible content.

app/page.tsx

This is the first route-specific screen for the / address.

app/page.tsx
export default function Home() {
  return (
    <>
      <h1>Welcome to EduShop</h1>
      <p>Learn by building a real storefront, one feature at a time.</p>
    </>
  );
}

Read the code

  1. The default export returns a React component. Its name Home is descriptive; the filename page.tsx creates the route.
  2. The fragment groups the heading and paragraph. The root layout wraps this content in its document and main element.
  3. The text is static source data at this stage. There are no hooks, browser handlers or database reads.

4. Run it and check your result

  • The terminal reports a local address and the home page displays Welcome to EduShop.
  • Changing the heading updates the browser. About and Contact will work in the next lesson.

5. Predict, change and observe

Temporarily replace {children} in the layout with the text Layout only. Predict the result, then restore it.

Compare your prediction with the expected result

What you should observe

The navigation and Layout only remain, but the home-page heading disappears.

Why it happens

The page can exist and be selected correctly, yet stay invisible if the layout does not render its children. This is a composition problem rather than a missing route.

6. Troubleshoot with evidence

A common mistake

Run commands from the folder containing package.json. If port 3000 is occupied, stop the other tutorial server or use npm run dev -- --port 3001.

npm reports that package.json is missing.

Likely cause: The terminal is in the ZIP’s parent folder or another directory.

Check and correct: Open the extracted edushop folder that contains package.json, then run npm install there.

Your edits do not appear.

Likely cause: You may be viewing another port or running the production server.

Check and correct: Use the address printed by npm run dev. If you used start, stop it and start the development server.

7. Practice and explain it back

Your independent task

Add a second paragraph describing three products your store will sell.

Attempt these questions before opening the explanations. Use the source above to justify your answer.

Does renaming the Home function change the URL?

No. app/page.tsx establishes the route. The function name can improve readability but does not define the URL.

Why do we keep both layout.tsx and page.tsx?

The layout owns shared document structure. The page owns the content for one route. Keeping those responsibilities separate prevents repeated navigation and makes later routes simpler.