@cerner/duplicate-package-checker-webpack-plugin

中文文档
2.6.0发布于 2023/09/28Apache-2.0CommonJS近 30 天下载 13.3万

Webpack plugin that warns you when multiple versions of the same package exist in a build.

原始使用文档
文档目录12 节
  1. Why?
  2. Install
  3. Configuration
  4. Strict mode
  5. Node version support
  6. Resolving duplicate packages in your bundle
  7. Webpack resolve.alias
  8. Yarn install --flat
  9. Yarn resolutions
  10. NPM Dedupe
  11. Bump your dependencies
  12. File issues!

🕵 duplicate-package-checker-webpack-plugin

NPM version Downloads Build Status Dependencies status devDependencies status

Webpack plugin that warns when your bundle contains multiple versions of the same package.

duplicate-package-checker-webpack-plugin

This package is a modified fork of darrenscerri's duplicate-package-checker-webpack-plugin.

Why?

It might be possible that a single package gets included multiple times in a Webpack bundle due to different package versions. This situation may happen without any warning, resulting in extra bloat in your bundle and may lead to hard-to-find bugs.

This plugin will warn you of such cases to minimize bundle size and avoid bugs caused by unintended duplicate packages.

Motivation: https://github.com/webpack/webpack/issues/385 and https://github.com/webpack/webpack/issues/646.

Install

npm install @cerner/duplicate-package-checker-webpack-plugin --save-dev

Configuration

Add the plugin to your webpack config:

var DuplicatePackageCheckerPlugin = require("@cerner/duplicate-package-checker-webpack-plugin");

module.exports = {
  plugins: [new DuplicatePackageCheckerPlugin()]
};

You can also pass an object with configurable options:

new DuplicatePackageCheckerPlugin({
  // Also show module that is requiring each duplicate package (default: false)
  verbose: true,
  // Emit errors instead of warnings (default: false)
  emitError: true,
  // Show help message if duplicate packages are found (default: true)
  showHelp: false,
  // Warn also if major versions differ (default: true)
  strict: false,
  /**
   * Exclude instances of packages from the results.
   * If all instances of a package are excluded, or all instances except one,
   * then the package is no longer considered duplicated and won't be emitted as a warning/error.
   * @param {Object} instance
   * @param {string} instance.name The name of the package
   * @param {string} instance.version The version of the package
   * @param {string} instance.path Absolute path to the package
   * @param {?string} instance.issuer Absolute path to the module that requested the package
   * @returns {boolean} true to exclude the instance, false otherwise
   */
  exclude: instance => instance.name === "fbjs",
  // Emit errors (regardless of emitError value) when the specified packages are duplicated (default: [])
  alwaysEmitErrorsFor: ['react', 'react-router'],
});

Strict mode

Strict mode warns when multiple packages with different major versions (such as v1.0.0 vs v2.0.0) exist in the bundle.

Packages with different major versions introduce backward incompatible changes and require either interventions on third-party packages or unsafe workarounds (such as resolving differing package major versions dependencies with a single version).

It is suggested that strict mode is kept enabled since this improves visibility into your bundle and can help in solving and identifying potential issues.

Node version support

This package was developed and tested using Node 10 up to Node 14. Consumers using Node 16 or greater are advised to use it at their own risk since those versions are not officially supported due to lack of thorough testing.

Resolving duplicate packages in your bundle

There are multiple ways you can go about resolving duplicate packages in your bundle, the right solution mostly depends on what tools you're using and on each particular case.

Webpack resolve.alias

Add an entry in resolve.alias which will configure Webpack to route any package references to a single specified path.

For example, if Lodash is duplicated in your bundle, the following configuration would render all Lodash imports to always refer to the Lodash instance found at ./node_modules/lodash.

alias: {
  lodash: path.resolve(__dirname, 'node_modules/lodash'),
}

Note: Aliasing packages with different major versions may break your app. Use only if you're sure that all required versions are compatible, at least in the context of your app

Yarn install --flat

Yarn allows flat installations (yarn install --flat) which will only allow one version of each package to be installed.

Yarn resolutions

If you want more control over your overridden dependency versions and don't feel like using yarn install --flat, yarn supports "selective version resolution" which allows you to enforce specific versions for each dependency.

package.json

{
  "dependencies": {
    "lodash": "4.17.0",
    "old-package-with-old-lodash": "*"
  },
  "resolutions": {
    "old-package-with-old-lodash/lodash": "4.17.0"
  }
}

NPM Dedupe

If you use NPM and not Yarn, you can try running npm dedupe. NPM may leave multiple versions of the same package installed even if a single version satisfies each semver of all of its dependants.

Bump your dependencies

If your project is using an old version of a package and a dependency is using a newer version of that package, consider upgrading your project to use the newer version.

File issues!

If your project has a dependency and it's using an outdated version of a package, file an issue and notify the author to update the dependencies. Let's help keep our projects green and our applications secure, performant and bug-free!

版本与发布记录

@cerner/duplicate-package-checker-webpack-plugin 共发布 11 个版本,首次发布于 2019/06/22;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.6.0latest2023/09/283 年前
2.5.02023/09/013 年前
2.4.22023/08/313 年前
2.4.12023/08/253 年前
2.4.02023/08/253 年前
2.3.02022/02/124 年前

关于 @cerner/duplicate-package-checker-webpack-plugin 的常见问题

@cerner/duplicate-package-checker-webpack-plugin 是什么?

Webpack plugin that warns you when multiple versions of the same package exist in a build.

如何安装 @cerner/duplicate-package-checker-webpack-plugin?

在项目目录运行 npm install @cerner/duplicate-package-checker-webpack-plugin,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:webpack(^4.30.0 || ^5.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@cerner/duplicate-package-checker-webpack-plugin 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/cerner__duplicate-package-checker-webpack-plugin 是否存在。

@cerner/duplicate-package-checker-webpack-plugin 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@cerner/duplicate-package-checker-webpack-plugin 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js 10 || 12 || 14。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@cerner/duplicate-package-checker-webpack-plugin 可以商用吗?

@cerner/duplicate-package-checker-webpack-plugin 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@cerner/duplicate-package-checker-webpack-plugin 的下载量有多少?

根据 npm 官方下载统计,@cerner/duplicate-package-checker-webpack-plugin 在 2026/10/10 – 2026/11/08 这 30 天内被下载 133,145 次,日均约 4,438 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cerner/duplicate-package-checker-webpack-plugin 还在维护吗?

最新版本 2.6.0 发布于 2023/09/28(3 年前);至今共发布 11 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/06/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。