Sub topic 2
Creating a project and reading its layout
This chapter assumes you know React components, TypeScript basics, and Node.js and npm. You will create a React Router framework mode project and learn what each generated file does. The chapter is about 470 words.
Create the project
Use the official template. It needs a current Node.js LTS release.
npx create-react-router@latest my-app
cd my-app
npm run devThe prompts ask whether to initialize git and install dependencies. Accept both. The dev server starts at http://localhost:5173 and reloads on edits.
Read the layout
The template produces this structure:
my-app/
app/
routes/
home.tsx
app.css
root.tsx
routes.ts
welcome/
public/
package.json
react-router.config.ts
tsconfig.json
vite.config.tsThe files that matter most:
| File | Purpose |
|---|---|
| `app/routes.ts` | Maps URL paths to route module files. |
| `app/root.tsx` | The root route. It renders the HTML document shell that wraps every page. |
| `app/routes/` | Route modules. Each file can export a loader, an action, and a component. |
| `react-router.config.ts` | Framework settings, such as `ssr` and `prerender`. |
| `vite.config.ts` | Vite plugins, including the React Router plugin. |
| `public/` | Static files served as they are, such as the favicon. |
React Router framework mode runs on Vite. Vite handles bundling and the dev server. The React Router plugin adds route handling, server rendering, and type generation.
Follow one request
A route module is a file that the router loads for a URL. The template's routes.ts has one entry:
import { type RouteConfig, index } from "@react-router/dev/routes";
export default [index("routes/home.tsx")] satisfies RouteConfig;The index call says that / renders routes/home.tsx. The path is relative to the app directory. The route module then runs inside the root route.
Check the configuration
Open react-router.config.ts. A new project looks like this:
import type { Config } from "@react-router/dev/config";
export default {
ssr: true,
} satisfies Config;With ssr: true, the server renders HTML for each request. A later chapter covers prerendering and SPA mode.
Common errors
- Route file not found. A path in
routes.tsmust point to an existing file underapp/. A typo causes a build error. - Editing the wrong file for a new page. A new file in
app/routes/does nothing until you add it toroutes.ts. This is explicit configuration, not file-based routing. - Old Node.js version. Install failures or syntax errors at startup often come from an outdated Node.js.
Try a change
Add a second route to see how the pieces connect. Create app/routes/about.tsx:
export default function About() {
return <h1>About</h1>;
}Then register it in app/routes.ts:
import { type RouteConfig, index, route } from "@react-router/dev/routes";
export default [
index("routes/home.tsx"),
route("about", "routes/about.tsx"),
] satisfies RouteConfig;Visit /about and the heading appears.
Exercise
You create app/routes/pricing.tsx with a default-exported component and open /pricing, but you get a 404. What is missing, and what line fixes it?
Show answer
The route is not registered. Add route("pricing", "routes/pricing.tsx") to the array in app/routes.ts. Framework mode uses explicit route configuration, so a file in app/routes/ is not served until it is listed there.