Loading…
Loading documentation...
Create a Next.js 16 app with TypeScript and the App Router, add a page of your own, write a first test with Vitest, and build it for production.
Commands checked on 2026-10-13 with Next.js 16.4, React 19.3, TypeScript 5.9, Vitest 5.0, Node.js 24, npm 11.
A Next.js application in a folder of your own, with TypeScript in strict mode, ESLint, two pages, one passing test, and a production build that serves both pages and answers 404 for an address that does not exist. Every later guide in this library starts from a project like this one.
node -v and npm -v.Run the generator with its choices given as flags, so that it asks nothing and two people get the same project:
npx create-next-app@latest my-app --typescript --eslint --app --src-dir --no-tailwind --import-alias "@/*" --use-npm --yes
cd my-app
It installs Next.js, React and TypeScript, starts a git repository and ends with Success! Created my-app. On our run this took 27 seconds.
What the flags chose: TypeScript, ESLint as the linter, the App Router, source code under src/, no Tailwind CSS, the import alias @/ for src/, and npm as the package manager. --yes accepts the default for everything else.
ls -a
cat package.json
The parts that matter:
src/app/layout.tsx wraps every page. src/app/page.tsx is the home page.package.json has four scripts: dev, build, start and lint.tsconfig.json already has "strict": true.AGENTS.md tells AI coding agents to read the documentation that ships inside the installed package, under node_modules/next/dist/docs/. That folder matches the version you installed, which the web may not.Replace the content of src/app/page.tsx:
import Link from 'next/link'
export default function Home() {
return (
<main>
<h1>My app</h1>
<p>Built with Next.js and TypeScript.</p>
<Link href="/about">About</Link>
</main>
)
}
Add a second page. A folder under src/app is an address, and its page.tsx is what that address shows. Create src/app/about/page.tsx:
import type { Metadata } from 'next'
export const metadata: Metadata = { title: 'About' }
export default function About() {
return <h1>About</h1>
}
The generator installs type definitions for Node.js 20. You run Node.js 24, and the test tools in the next step refuse the older types. Install the matching major version:
npm install -D @types/node@24
npm install -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom vite-tsconfig-paths
Create vitest.config.mts in the project folder:
import { defineConfig } from 'vitest/config'
import react from '@vitejs/plugin-react'
import tsconfigPaths from 'vite-tsconfig-paths'
export default defineConfig({
plugins: [tsconfigPaths(), react()],
test: {
environment: 'jsdom',
},
})
Add two scripts. vitest run runs the tests once and exits, which is what a build server needs; plain vitest keeps watching your files.
npm pkg set scripts.test="vitest run" scripts.typecheck="tsc --noEmit"
Create src/app/page.test.tsx next to the page it tests:
import { expect, test } from 'vitest'
import { render, screen } from '@testing-library/react'
import Home from './page'
test('the home page names the app and links to About', () => {
render(<Home />)
expect(screen.getByRole('heading', { level: 1, name: 'My app' })).toBeDefined()
expect(screen.getByRole('link', { name: 'About' }).getAttribute('href')).toBe('/about')
})
The test finds elements the way a person does: a heading by its text, a link by its name. Run it:
npm test
You should see Test Files 1 passed (1) and Tests 1 passed (1).
A test you have never seen fail may be testing nothing. Change 'My app' in the test to 'Another app' and run npm test again. It must fail with:
TestingLibraryElementError: Unable to find an accessible element with the role "heading" and name "Another app"
Change it back.
npm run typecheck
npm run lint
npm run build
typecheck and lint print nothing when all is well. build ends with the list of routes:
Route (app)
┌ ○ /
├ ○ /_not-found
└ ○ /about
○ (Static) prerendered as static content
Start the production server and ask it for three addresses from a second terminal:
npm run start
curl -s -o /dev/null -w '%{http_code}\n' http://localhost:3000/
curl -s -o /dev/null -w '%{http_code}\n' http://localhost:3000/about
curl -s -o /dev/null -w '%{http_code}\n' http://localhost:3000/nope
The answers are 200, 200 and 404. Stop the server with Ctrl+C.
While you write code, use npm run dev instead: it starts in under a second and reloads the page when you save a file.
npm error ERESOLVE while installing Vitest. The report names @types/node: Vitest 5 wants the types for Node.js 22 or 24 and newer, and the project has the types for Node.js 20. Do step 4 first. Do not reach for --force or --legacy-peer-deps: they install a tree the packages say they do not support.sh: 1: vitest: not found. The install in step 5 failed and you did not notice. Scroll up to the first npm error.Cannot find module 'vitest/config' from npm run typecheck. The same cause: the test tools are not installed.npm run start -- -p 3001.npm audit reports high severity findings in the new project. On the day we ran this guide it reported five, all in the linter's own dependencies, and none in what the application ships. The guide on reading an audit report explains how to tell the two apart.strict on from the first day. Turning it on later means fixing every file at once.typecheck, lint, test and build before every commit. Each catches what the others cannot.package-lock.json. It is what makes the next install the same as this one.Every Servers Are Up virtual machine and container includes an integrated HTML5 browser-based SSH, RDP, and VNC console in your dashboard, default-off inbound firewall rules with per-IP allow-lists, and full programmatic control via REST API v1 and Model Context Protocol (MCP). Explore our Virtual Datacenter Plans or reach out to Servers Are Up Support.