日本語

How to Fix Duplicated JavaScript in Lighthouse

​​Lighthouse​esbuild,JavaScript,npm,Performance

When Lighthouse reports "Duplicated JavaScript", the same library is included in two or more JavaScript files. This audit is useful when a site has multiple entry points and you want to reduce bundle size to speed up the page. If you split the shared library into its own file with code splitting, the warning goes away.

On the page I tested, adding --splitting to esbuild was enough to pass the audit. The total JavaScript transfer size dropped from 15,661 bytes to 8,859 bytes. When two versions of a library were included, I aligned the versions with npm's overrides and then bundled them the same way, which removed the duplication.

Note

The test environment was Ubuntu 24.04, Node.js 22 (npm 10.9.9), Lighthouse 13.6.0 (npm package), esbuild 0.28.2, and date-fns 4.4.0 (with 3.6.0 and 4.3.0 as comparison targets). I ran Lighthouse from the command line with the default settings (mobile), limited to the performance category, once per page. Chrome was Chrome for Testing 153, and I used Playwright 1.63.0 to check that the pages rendered and to take screenshots. The pages were served by a small Node.js server running on the same VM, with gzip compression. I did not test webpack, Vite, or Rollup configurations, version alignment with pnpm or Yarn, the Lighthouse panel in DevTools, PageSpeed Insights, desktop settings, or the Lighthouse Treemap view.

Bundling per entry point duplicates the same library

The test page only shows a cat's meal log and the number of days until the next health checkup. I split the JavaScript into two entry points. app.js displays the meal list, and calendar.js displays the health checkup date. Both use the format function and the Japanese locale (ja) from date-fns to display dates.

js
// src/calendar.js
import { format, addDays, differenceInCalendarDays } from 'date-fns';
import { ja } from 'date-fns/locale';

I used esbuild to bundle each of these into a single file. Lighthouse uses source maps to find out which modules are included in each file (the Lighthouse source also lists source maps as a requirement for this audit), so I built source maps with --sourcemap and served them along with the bundles.

console
$ npx esbuild src/app.js src/calendar.js --bundle --minify --sourcemap --format=esm --outdir=dist-separate

  dist-separate/app.js            26.0kb
  dist-separate/calendar.js       25.9kb
  dist-separate/app.js.map       155.4kb
  dist-separate/calendar.js.map  155.1kb

(The timing line at the end is omitted.)

Both files included date-fns's format.js and the locale file ja.js. I ran Lighthouse on this page, then used a small script (summarize.mjs) to extract the "Duplicated JavaScript" section (duplicated-javascript-insight in audits) from the JSON result. The output was:

console
$ npx lighthouse http://localhost:8080/ --only-categories=performance --output=json --output=html --output-path=./out/separate --chrome-flags="--headless=new --no-sandbox" --quiet
exit: 0
$ node tools/summarize.mjs out/separate.report.json
lighthouseVersion: 13.6.0  formFactor: mobile  throttling: simulate
performance score: 100  FCP: 0.6 s  LCP: 0.6 s  TBT: 10 ms
duplicated-javascript-insight: score=0.5 scoreDisplayMode=metricSavings title="Duplicated JavaScript" displayValue="Est savings of 4 KiB" metricSavings={"FCP":0,"LCP":0}
  node_modules/date-fns  wastedBytes=7668
    http://localhost:8080/app.js  --
    http://localhost:8080/calendar.js  7668
  weight=0 group=insights
  request /app.js  transferSize=7841  resourceSize=26596
  request /calendar.js  transferSize=7820  resourceSize=26560

When you expand the item in the HTML report, the row for node_modules/date-fns is followed by the two files that contain it.

Lighthouse report with the Duplicated JavaScript audit expanded. The heading shows "Est savings of 4 KiB". The table lists node_modules/date-fns at 10 KiB, with /app.js shown as "--" and /calendar.js at 10 KiB below it.
Report when each entry point is bundled separately

The table treats the largest copy of date-fns (here, in app.js) as the baseline, shown as --, and counts the copies in other files as duplicated bytes. The date-fns copy in calendar.js is 7,668 bytes in transfer size.

This audit does not affect the performance score. In Lighthouse 13.6.0 it is in the Insights group, with a weight of 0. The page I tested scored 100 even with this warning present.

Why "4 KiB" and "10 KiB" look inconsistent

The report heading says "Est savings of 4 KiB", but the table says "10 KiB" for the same duplication. Looking at the Lighthouse 13.6.0 source, the two numbers are calculated differently.

The table value is the date-fns total summed per package. 7,668 bytes is about 7.5 KiB, but this column rounds to 10 KiB increments (granularity: 10), so it shows "10 KiB".

The heading value is calculated by comparing files one at a time. Files smaller than 512 bytes in transfer size are not counted, and neither are files smaller than 10% of the largest copy. Using the esbuild metafile, I checked the size of each date-fns file in calendar.js. I estimated each file's transfer size by multiplying its uncompressed size by the ratio of calendar.js's transfer size to its uncompressed size (7,820 ÷ 26,560 ≈ 0.29). Four files were 512 bytes or larger (the ratio line is omitted):

console
  node_modules/date-fns/_lib/format/formatters.js  6895 -> 2030
  node_modules/date-fns/locale/ja/_lib/localize.js  2702 -> 796
  node_modules/date-fns/locale/en-US/_lib/localize.js  1988 -> 585
  node_modules/date-fns/locale/en-US/_lib/match.js  1859 -> 547
files=44  over512=4  sumAll=7739  sumOver512=3958

These four files add up to 3,958 bytes, which matches the numericValue of 3,958 bytes in the Lighthouse result. Many small files that overlap do not show up much in the heading figure. For deciding whether to fix something, the table value is the more reliable guide.

Splitting the shared library into one file with code splitting

Chrome's official explanation recommends splitting large libraries used by multiple files into a shared chunk. In esbuild, you do this with --splitting. As the esbuild help says, "currently only for esm", so use it together with --format=esm. Without --format=esm, I got the error Splitting currently only works with the "esm" format.

console
$ npx esbuild src/app.js src/calendar.js --bundle --minify --sourcemap --format=esm --splitting --outdir=dist-split

  dist-split/chunk-T7DAF6C5.js       25.7kb
  dist-split/app.js                   448b
  dist-split/calendar.js              422b

(The source map lines and the timing line at the end are omitted.)

date-fns was emitted into chunk-T7DAF6C5.js, and app.js and calendar.js became small files that import from it. The page's HTML still loads the two entry points with <script type="module">, and I did not change it.

console
$ node tools/summarize.mjs out/split.report.json
duplicated-javascript-insight: score=1 scoreDisplayMode=metricSavings title="Duplicated JavaScript" displayValue="" metricSavings={"FCP":0,"LCP":0}
  weight=0 group=insights
  request /app.js  transferSize=593  resourceSize=448
  request /calendar.js  transferSize=584  resourceSize=422
  request /chunk-T7DAF6C5.js  transferSize=7682  resourceSize=26296

(The Lighthouse version and score lines at the top are omitted.)

The table is empty, and the audit passes (score=1). The total JavaScript transfer size dropped from 15,661 bytes to 8,859 bytes. I opened the page in Playwright to check. The meal list (3 rows) and the health checkup text were the same as before, and there were 0 console errors.

Different versions still show up in the table even when the audit passes

Next, I moved the calendar.js logic into a small library I made for this test (cat-calendar). I packaged it with npm pack and installed it in the project. This library depends on date-fns ^3.6.0, so npm installs 3.6.0 under the library separately from 4.4.0.

console
$ npm ls date-fns
+-- cat-calendar@1.0.0
| `-- date-fns@3.6.0
`-- date-fns@4.4.0

(The project path on the first line is omitted.)

In this state, adding --splitting did not produce a shared chunk. The two versions are at different file paths, so esbuild treats them as separate modules. app.js included date-fns 4.4.0, and calendar.js included date-fns 3.6.0.

Lighthouse marked the audit as passed (score=1), but the table still lists node_modules/date-fns at 7,586 bytes.

Lighthouse report with the Duplicated JavaScript audit expanded. A green pass icon appears, but the table still lists node_modules/date-fns at 10 KiB, with /app.js shown as "--" and /calendar.js at 10 KiB.
Report with two date-fns versions. The audit passed, but date-fns still appears in the table
console
$ node tools/summarize.mjs out/twover.report.json
duplicated-javascript-insight: score=1 scoreDisplayMode=metricSavings title="Duplicated JavaScript" displayValue="" metricSavings={"FCP":0,"LCP":0}
  node_modules/date-fns  wastedBytes=7586
    http://localhost:8080/app.js  --
    http://localhost:8080/calendar.js  7586

(The first two lines and the last three lines are omitted.)

The cause is the difference in file names recorded in the source maps. In the 4.4.0 copy the file is format.js, and in the 3.6.0 copy it is format.mjs.

console
dist-twover/app.js.map: 45 sources
  ../node_modules/date-fns/format.js
  ../node_modules/date-fns/locale/ja.js
dist-twover/calendar.js.map: 44 sources
  ../node_modules/cat-calendar/node_modules/date-fns/format.mjs
  ../node_modules/cat-calendar/node_modules/date-fns/locale/ja.mjs

In the Lighthouse 13.6.0 source, file paths are compared starting from the last node_modules segment. cat-calendar/node_modules/date-fns/format.js and date-fns/format.js are treated as the same file, but .mjs and .js are different files. Pass or fail depends on whether at least one file of 512 bytes or larger overlaps. The table, however, counts by package name (node_modules/date-fns). So when the versions have different file names, the audit can pass while the table still lists the duplication.

To confirm this, I changed the library to use 4.3.0 and ran the same test. The 4.3.0 file names match 4.4.0 (format.js). This time the audit failed (score=0.5, Est savings of 4 KiB), and the table listed 7,654 bytes.

Point

Even when the result shows a pass, a row in the table means the duplication is still there. When different versions of a library are included, expand the passed item and check its contents.

Aligning versions with npm overrides

Chrome's official explanation also recommends using only one version of a large library, even within a single file. If you cannot change the version that a dependency uses, npm's overrides lets you specify which version gets installed under that dependency. As described in npm's package.json documentation, writing a name with a $ prefix ($date-fns) uses the same version that your project directly depends on for date-fns.

json
{
  "dependencies": {
    "cat-calendar": "file:pkg/cat-calendar/cat-calendar-1.0.0.tgz",
    "date-fns": "4.4.0"
  },
  "overrides": {
    "cat-calendar": {
      "date-fns": "$date-fns"
    }
  }
}

(Test-only tools such as esbuild are omitted from dependencies.)

Adding the overrides entry and running npm i alone did not change the version. A leftover package-lock.json kept npm at 3.6.0, and npm ls reported it as invalid.

console
$ npm i
up to date in 235ms
$ npm ls date-fns
+-- cat-calendar@1.0.0 overridden
| `-- date-fns@3.6.0 invalid: "4.4.0" from node_modules/cat-calendar overridden
`-- date-fns@4.4.0

npm error code ELSPROBLEMS
npm error invalid: date-fns@3.6.0 /path/to/work/node_modules/cat-calendar/node_modules/date-fns

(The project line at the top of npm ls is omitted, and the path is replaced with /path/to/work. In this section, the npm i command was run with --no-fund --no-audit during the test.)

In the test, deleting node_modules and package-lock.json and reinstalling aligned everything to a single 4.4.0 (deduped).

console
$ rm -rf node_modules package-lock.json && npm i
added 124 packages in 6s
$ npm ls date-fns
+-- cat-calendar@1.0.0 overridden
| `-- date-fns@4.4.0 deduped
`-- date-fns@4.4.0 overridden

(The first line of npm ls is omitted here as well.)

Warning

overrides installs a version that differs from the one the library's package.json asks for (here, ^3.6.0). In my test, the health checkup text displayed the same as before. After aligning versions, check that every screen using the library still works as it did before.

After that, I bundled again with --splitting. date-fns went into a single shared file, and the Lighthouse table was empty.

Build tried Result Estimated savings (heading) date-fns in table Total JS transfer
Separate bundle per entry point Failed 4 KiB 7,668 15,661
--splitting Passed None None 8,859
Two versions (3.6.0 and 4.4.0), --splitting Passed None 7,586 15,579
Aligned with overrides, --splitting Passed None None 8,865
Reference: two versions (4.3.0 and 4.4.0), --splitting Failed 4 KiB 7,654 15,652

(All sizes are in bytes. Transfer size is the total of transferSize in the network-requests section of the Lighthouse result, with the server returning gzip-compressed responses. All pages rendered the same way, and there were 0 console errors.)

As in the first test, even with a single version, separate bundles per entry point leave the duplication in place. Aligning the version and also applying code splitting caused date-fns to be delivered only once.

​Posted by 86