Skip to the code

Find duplicate packages in a bundle

When two dependencies need versions of a package that don't overlap, the package manager installs both, and the bundler bundles both: the tea shop ends up shipping two copies of lodash. duplicate-package-checker-webpack-plugin warns about it at build time, and inspectpack's DuplicatesPlugin reports the wasted bytes. Write duplicatePackages(modules, versions). modules are the bundle's modules, { path, bytes }, with paths relative to the project (node_modules/lodash/debounce.js, src/menu.js). versions maps each installed package's folder (node_modules/lodash) to its version, and has every folder the modules are in.

A module belongs to the package named right after the last node_modules/ in its path: one folder, or two when the first starts with @ (a scoped package such as @leaf/ui). The package's folder is the path up to and including that name, so node_modules/carousel/node_modules/lodash/fp.js is in node_modules/carousel/node_modules/lodash, and pnpm's node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/fp.js is in lodash as well. A module with no node_modules/ in its path is the app's own code and doesn't count.

Each folder is one copy of its package, and a copy's bytes is the sum of its modules' bytes. Return the packages that have copies of more than one version, as { name, bytes, copies }: bytes is the sum of all its copies, and copies are { path, version, bytes } for every copy (its folder, its version, its bytes), largest first, by path with < when two weigh the same. Two copies of the same version are both listed, but they don't make a package a duplicate on their own. Sort the packages by bytes, largest first, then by name with <.

duplicatePackages(
  [
    { path: "src/menu.js", bytes: 9000 },
    { path: "node_modules/lodash/debounce.js", bytes: 3000 },
    { path: "node_modules/lodash/throttle.js", bytes: 2000 },
    { path: "node_modules/old-carousel/index.js", bytes: 8000 },
    { path: "node_modules/old-carousel/node_modules/lodash/lodash.js", bytes: 70000 },
    { path: "node_modules/@leaf/ui/index.js", bytes: 12000 },
  ],
  {
    "node_modules/lodash": "4.17.21",
    "node_modules/old-carousel": "1.2.0",
    "node_modules/old-carousel/node_modules/lodash": "3.10.1",
    "node_modules/@leaf/ui": "2.0.0",
  },
);
// [{
//   name: "lodash",
//   bytes: 75000,
//   copies: [
//     { path: "node_modules/old-carousel/node_modules/lodash", version: "3.10.1", bytes: 70000 },
//     { path: "node_modules/lodash", version: "4.17.21", bytes: 5000 },
//   ],
// }]

What the tests check

  1. finds a package bundled in two versions, with what each copy weighs
    const modules = [
      { path: "src/menu.js", bytes: 9000 },
      { path: "node_modules/lodash/debounce.js", bytes: 3000 },
      { path: "node_modules/lodash/throttle.js", bytes: 2000 },
      { path: "node_modules/old-carousel/index.js", bytes: 8000 },
      { path: "node_modules/old-carousel/node_modules/lodash/lodash.js", bytes: 70000 },
      { path: "node_modules/@leaf/ui/index.js", bytes: 12000 },
    ];
    const versions = {
      "node_modules/lodash": "4.17.21",
      "node_modules/old-carousel": "1.2.0",
      "node_modules/old-carousel/node_modules/lodash": "3.10.1",
      "node_modules/@leaf/ui": "2.0.0",
    };
    expect(duplicatePackages(modules, versions)).toEqual([
      {
        name: "lodash",
        bytes: 75000,
        copies: [
          { path: "node_modules/old-carousel/node_modules/lodash", version: "3.10.1", bytes: 70000 },
          { path: "node_modules/lodash", version: "4.17.21", bytes: 5000 },
        ],
      },
    ]);
    
  2. a module belongs to the package after the last node_modules, however deep
    const modules = [
      { path: "node_modules/tea-slider/node_modules/swipe-kit/node_modules/tiny-emitter/index.js", bytes: 1500 },
      { path: "node_modules/tiny-emitter/index.js", bytes: 1200 },
      { path: "node_modules/.pnpm/date-fns@2.30.0/node_modules/date-fns/format.js", bytes: 9000 },
      { path: "node_modules/.pnpm/date-fns@3.6.0/node_modules/date-fns/format.js", bytes: 7000 },
    ];
    const versions = {
      "node_modules/tea-slider/node_modules/swipe-kit/node_modules/tiny-emitter": "1.0.0",
      "node_modules/tiny-emitter": "2.1.0",
      "node_modules/.pnpm/date-fns@2.30.0/node_modules/date-fns": "2.30.0",
      "node_modules/.pnpm/date-fns@3.6.0/node_modules/date-fns": "3.6.0",
    };
    expect(duplicatePackages(modules, versions)).toEqual([
      {
        name: "date-fns",
        bytes: 16000,
        copies: [
          { path: "node_modules/.pnpm/date-fns@2.30.0/node_modules/date-fns", version: "2.30.0", bytes: 9000 },
          { path: "node_modules/.pnpm/date-fns@3.6.0/node_modules/date-fns", version: "3.6.0", bytes: 7000 },
        ],
      },
      {
        name: "tiny-emitter",
        bytes: 2700,
        copies: [
          { path: "node_modules/tea-slider/node_modules/swipe-kit/node_modules/tiny-emitter", version: "1.0.0", bytes: 1500 },
          { path: "node_modules/tiny-emitter", version: "2.1.0", bytes: 1200 },
        ],
      },
    ]);
    
  3. a scoped package's name is its scope and the folder after it
    const modules = [
      { path: "node_modules/@leaf/icons/leaf.js", bytes: 4000 },
      { path: "node_modules/@leaf/icons/cup.js", bytes: 2000 },
      { path: "node_modules/@leaf/ui/button.js", bytes: 6000 },
      { path: "node_modules/@leaf/ui/node_modules/@leaf/icons/leaf.js", bytes: 5000 },
      { path: "node_modules/icons/index.js", bytes: 3000 },
    ];
    const versions = {
      "node_modules/@leaf/icons": "3.1.0",
      "node_modules/@leaf/ui": "2.0.0",
      "node_modules/@leaf/ui/node_modules/@leaf/icons": "2.4.0",
      "node_modules/icons": "1.0.0",
    };
    expect(duplicatePackages(modules, versions)).toEqual([
      {
        name: "@leaf/icons",
        bytes: 11000,
        copies: [
          { path: "node_modules/@leaf/icons", version: "3.1.0", bytes: 6000 },
          { path: "node_modules/@leaf/ui/node_modules/@leaf/icons", version: "2.4.0", bytes: 5000 },
        ],
      },
    ]);
    
  4. copies of one version aren't a duplicate on their own, but are listed with another version
    const modules = [
      { path: "node_modules/tea-utils/index.js", bytes: 4000 },
      { path: "node_modules/cart/index.js", bytes: 2500 },
      { path: "node_modules/cart/node_modules/tea-utils/index.js", bytes: 4000 },
    ];
    const versions = { "node_modules/tea-utils": "1.4.0", "node_modules/cart": "5.0.0", "node_modules/cart/node_modules/tea-utils": "1.4.0" };
    expect(duplicatePackages(modules, versions)).toEqual([]);
    const more = [...modules, { path: "node_modules/reviews/node_modules/tea-utils/index.js", bytes: 3500 }];
    expect(duplicatePackages(more, { ...versions, "node_modules/reviews/node_modules/tea-utils": "0.9.2" })).toEqual([
      {
        name: "tea-utils",
        bytes: 11500,
        copies: [
          { path: "node_modules/cart/node_modules/tea-utils", version: "1.4.0", bytes: 4000 },
          { path: "node_modules/tea-utils", version: "1.4.0", bytes: 4000 },
          { path: "node_modules/reviews/node_modules/tea-utils", version: "0.9.2", bytes: 3500 },
        ],
      },
    ]);
    
  5. sorts packages by their bytes, largest first, then by name
    const modules = [
      { path: "node_modules/tiny-emitter/index.js", bytes: 1000 },
      { path: "node_modules/slider/node_modules/tiny-emitter/index.js", bytes: 2000 },
      { path: "node_modules/lodash/lodash.js", bytes: 5000 },
      { path: "node_modules/slider/node_modules/lodash/lodash.js", bytes: 4000 },
      { path: "node_modules/dayjs/dayjs.js", bytes: 1500 },
      { path: "node_modules/slider/node_modules/dayjs/dayjs.js", bytes: 1500 },
    ];
    const versions = {
      "node_modules/tiny-emitter": "2.1.0",
      "node_modules/slider/node_modules/tiny-emitter": "1.0.0",
      "node_modules/lodash": "4.17.21",
      "node_modules/slider/node_modules/lodash": "3.10.1",
      "node_modules/dayjs": "1.11.13",
      "node_modules/slider/node_modules/dayjs": "1.8.0",
    };
    const result = duplicatePackages(modules, versions);
    expect(result.map((item) => [item.name, item.bytes])).toEqual([["lodash", 9000], ["dayjs", 3000], ["tiny-emitter", 3000]]);
    expect(result[1].copies.map((copy) => copy.path)).toEqual(["node_modules/dayjs", "node_modules/slider/node_modules/dayjs"]);
    
  6. the app's own modules don't count, and no modules is an empty array
    const modules = [
      { path: "src/lodash.js", bytes: 2000 },
      { path: "src/menu/lodash/index.js", bytes: 3000 },
      { path: "node_modules/lodash/lodash.js", bytes: 70000 },
    ];
    expect(duplicatePackages(modules, { "node_modules/lodash": "4.17.21" })).toEqual([]);
    expect(duplicatePackages([], {})).toEqual([]);
    

Learn more

solution.jsSaved in this browser

Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.

Ctrl + Enter runs the tests (⌘ + Enter on a Mac).