Next.js
Next.js 16.2.4 is a powerful React framework for building full-stack web applications. It offers features like server-side rendering (SSR), static site generation (SSG), incremental static regeneration (ISR), and a modern App Router for developing performant and scalable web experiences. The project maintains an active development pace with frequent canary releases leading to regular stable updates.
Common errors
-
Error: The "next" package requires Node.js version ">=20.9.0".
cause Your current Node.js version is older than 20.9.0.fixUpgrade your Node.js environment to version 20.9.0 or newer. You can use `nvm install 20` and `nvm use 20`, or `volta install node@20`. -
Error: Hydration failed because the initial UI does not match what was rendered on the server.
cause The HTML rendered by your React components on the server-side differs from what is rendered on the client-side, often due to client-side-only code executing prematurely on the server, or dynamic content changing between renders.fixEnsure components render identically on both server and client. Place client-only logic within `useEffect` hooks, use `typeof window` checks, or consider `suppressHydrationWarning` for unavoidable mismatches (with caution). -
Error: "use client" must be at the top of the file.
cause You are using client-side React features (e.g., `useState`, `useEffect`, event handlers) in an App Router component that is implicitly a Server Component, or the `'use client'` directive is not the very first line of the file.fixAdd `'use client';` as the absolute first line of the file to mark it as a Client Component, allowing client-side React features. Ensure there are no comments or other statements before it. -
Module not found: Can't resolve 'react'
cause The `react` or `react-dom` peer dependencies are not correctly installed or are incompatible with the Next.js version, or there are multiple instances of React causing conflicts.fixVerify that `react` and `react-dom` versions in your `package.json` match Next.js's peer dependency requirements (`^18.2.0 || ^19.0.0`). Delete your `node_modules` directory and `package-lock.json` (or `yarn.lock`), then run `npm install` (or `yarn install`) again.
Warnings
- breaking Next.js 16.x requires Node.js version 20.9.0 or newer. Running with an older Node.js version will prevent the application from starting.
- breaking Next.js 16.x has strict peer dependencies on React. Ensure your project uses React and React DOM versions `^18.2.0` or `^19.0.0`.
- gotcha For Pages Router applications, the Safari `?ts=` cache-buster URL parameter is now specifically scoped to CSS and font assets only. This might change caching behavior for other asset types if you were relying on its broader application.
Install
-
npm install next -
yarn add next -
pnpm add next
Imports
- Image
const Image = require('next/image');import Image from 'next/image';
Quickstart
import React from 'react';
// app/page.tsx
export default function HomePage() {
return (
<main className="flex min-h-screen flex-col items-center justify-center p-24">
<h1>Hello, Next.js!</h1>
<p>This is a simple server component rendered by Next.js 16.</p>
</main>
);
}