Three Next.js services I work on had quietly piled up hundreds of megabytes of dead weight in their production Docker images. Auditing them showed how much comes off with three straightforward steps.

1. Clean up dependencies vs. devDependencies

Production installs skip devDependencies, so anything miscategorized under dependencies ships into every CI build, registry push, and runtime container. A quick audit regularly catches test helpers, lint plugins, and build-only tools sitting in dependencies.

2. Enable output: "standalone"

While Webpack/Turbopack tree-shaking trims code inside your bundle, Next.js output: "standalone" uses @vercel/nft (Node File Tracing) to trace only the runtime files and node_modules slices your server actually imports, writing *.nft.json manifests explaining why each file was kept.

In production Dockerfiles, copy only .next/standalone, .next/static, and public/, not the full node_modules tree. (If you preload an APM tracer via NODE_OPTIONS, see Preloading APM Tracers in Next.js Standalone Builds.)

3. Audit *.nft.json and tune outputFileTracingExcludes

Run next build locally and inspect .next/standalone/node_modules. Whenever heavy unexpected packages show up, check *.nft.json to trace the import chain. Fix accidental app or package imports first, and for unavoidable third-party tracing bloat, exclude unused assets explicitly in next.config.js:

experimental: {
  outputFileTracingExcludes: {
    "*": [
      // Exclude build-only or unused native/browser binaries pulled in by tracing
    ],
  },
}

Measured impact across production services

Turning on standalone alone produced big reductions on our largest service:

  • Total image size: 2.20 GB -> 1.45 GB (-34%, -741 MB)
  • JavaScript files: 75,409 -> 3,654 (-95%)
  • NPM packages: 1,069 -> 78 (-93%)
  • node_modules size: 1.30 GB -> 116 MB (-91%)

Adding outputFileTracingExcludes pushed production node_modules down another 59-73% across all three services (landing around 30-31 MB and removing 1,100-1,700+ unused files per image), speeding up container pulls, startup, CI pipelines, and runtime memory usage.