What is bundle analysis in Next.js and how do you do it?
Bundle analysis visualizes which modules make up your JavaScript chunks and how large they are, so you can find heavy dependencies, duplicates and code that leaked into the client. In Next.js 16 use the built-in Turbopack analyzer (next analyze or next build --analyze); for webpack builds use @next/bundle-analyzer.
Turbopack analyzer (Next.js 16.1+)
- Introduced as
next experimental-analyzein 16.1; the CLI now providesnext analyzeandnext build --analyze. - Opens an interactive UI with client and server views, route filters and import chains that explain why a module is included.
- 16.4 added a route summary homepage, a sortable table view, snapshots you can diff over time, and highlighting of modules on the critical render path.
next analyze -owrites the data to.next/diagnostics/analyzefor CI artifacts.
Webpack analyzer
@next/bundle-analyzer wraps the config: withBundleAnalyzer({ enabled: process.env.ANALYZE === "true" })(nextConfig), then ANALYZE=true next build --webpack. It produces treemap reports for client, server and edge bundles.
What to look for
- Large libraries in client chunks: move usage to Server Components, lazy-load them, or swap for lighter alternatives (
dayjsinstead ofmoment). - Server-only code in the client bundle: a
"use client"boundary placed too high, or a missingserver-onlyguard. - Duplicate versions of the same package (check lockfile resolutions).
- Barrel imports pulling in whole icon sets.
Beyond bundles
Next.js 16 removed size and First Load JS from next build output because they were misleading with RSC. Measure real impact with Lighthouse, Chrome DevTools coverage and Core Web Vitals field data.
Code example
1// package.json scripts2// "analyze": "next analyze", // Turbopack, interactive UI3// "analyze:ci": "next analyze -o", // write report to .next/diagnostics/analyze4// "analyze:webpack": "ANALYZE=true next build --webpack"56// next.config.ts (webpack analyzer)7import type { NextConfig } from "next";8import bundleAnalyzer from "@next/bundle-analyzer";910const withBundleAnalyzer = bundleAnalyzer({ enabled: process.env.ANALYZE === "true" });1112const nextConfig: NextConfig = {13 // your config14};1516export default withBundleAnalyzer(nextConfig);1718// Typical fix after analysis:19// Before: import moment from "moment"; // ~300KB in a client chunk20// After: move date formatting into a Server Component, or use Intl.DateTimeFormat
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.