Next.jsMedium

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-analyze in 16.1; the CLI now provides next analyze and next 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 -o writes the data to .next/diagnostics/analyze for 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 (dayjs instead of moment).
  • Server-only code in the client bundle: a "use client" boundary placed too high, or a missing server-only guard.
  • 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

tsx
1// package.json scripts
2// "analyze": "next analyze", // Turbopack, interactive UI
3// "analyze:ci": "next analyze -o", // write report to .next/diagnostics/analyze
4// "analyze:webpack": "ANALYZE=true next build --webpack"
5
6// next.config.ts (webpack analyzer)
7import type { NextConfig } from "next";
8import bundleAnalyzer from "@next/bundle-analyzer";
9
10const withBundleAnalyzer = bundleAnalyzer({ enabled: process.env.ANALYZE === "true" });
11
12const nextConfig: NextConfig = {
13 // your config
14};
15
16export default withBundleAnalyzer(nextConfig);
17
18// Typical fix after analysis:
19// Before: import moment from "moment"; // ~300KB in a client chunk
20// 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.

Open in study app