Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

check-dependency-version-consistency

bmish236.5kMIT5.0.0TypeScript support: included

Ensures dependencies are on consistent versions across a monorepo.

dependencies, linter, monorepo, nodejs, npm, package.json, packages, pnpm, workspace, yarn

readme

check-dependency-version-consistency

npm version CI

This CLI tool enforces the following aspects of consistency across a monorepo with npm / pnpm / Yarn workspaces:

  1. Dependencies are on consistent versions. For example, every package in a workspace that has a dependency on eslint should specify the same version for it.
  2. Dependencies on local packages use the local packages directly instead of older versions of them. For example, if one package package1 in a workspace depends on another package package2 in the workspace, package1 should request the current version of package2.

Motivation

  • Supports a uniform developer experience. Like shared code formatting standards, consistent dependency versions reduce friction and make it easier for developers to jump around and work across packages in a monorepo.
  • Discourages piecemeal upgrades. When possible, a dependency should be upgraded everywhere it exists in a monorepo at the same time. Upgrades are also simpler and easier to perform when fewer versions are present.
  • Reduces unexpected behavior. Having old versions present can lead to confusion, e.g., a bug present in some parts of a monorepo but not others, or uncertainty about which version wins out when served to the browser.
  • Cuts down on bundle size. Avoid serving excess dependency code to the browser.
  • Saves disk space and installation time. Faster local development and CI.

Usage

To install:

yarn add --dev check-dependency-version-consistency

To run, use this command and optionally pass the path to the workspace root (where the package.json file containing workspaces or pnpm-workspace.yaml is located):

yarn check-dependency-version-consistency .

If there are no inconsistencies, the program will exit with success.

If there are any inconsistencies, the program will exit with failure and output the mismatching versions.

Example

package.json (workspace root):

{
  "workspaces": ["*"],
  "scripts": {
    "lint": "npm-run-all --continue-on-error --aggregate-output --parallel \"lint:*\"",
    "lint:dependency-versions": "check-dependency-version-consistency .",
    "lint:dependency-versions:fix": "npm-run-all \"lint:dependency-versions --fix\""
  },
  "devDependencies": {
    "check-dependency-version-consistency": "*",
    "npm-run-all": "*"
  }
}

package1/package.json:

{
  "name": "package1",
  "devDependencies": {
    "eslint": "^8.0.0"
  },
  "dependencies": {
    "package2": "^0.0.0"
  }
}

package2/package.json:

{
  "name": "package2",
  "version": "1.0.0",
  "devDependencies": {
    "eslint": "^7.0.0"
  }
}

package3/package.json:

{
  "name": "package3",
  "devDependencies": {
    "eslint": "^7.0.0"
  }
}

Output:

Found 2 dependencies with mismatching versions across the workspace. Fix with `--fix`.
╔════════╤════════╤════════════════════╗
║ eslintUsagesPackages           ║
╟────────┼────────┼────────────────────╢
║ ^8.0.0 │ 1      │ package1           ║
╟────────┼────────┼────────────────────╢
║ ^7.0.0 │ 2      │ package2, package3 ║
╚════════╧════════╧════════════════════╝
╔══════════╤════════╤══════════╗
║ package2UsagesPackages ║
╟──────────┼────────┼──────────╢
║ 1.0.0    │ 1      │ package2 ║
╟──────────┼────────┼──────────╢
║ ^0.0.0   │ 1      │ package1 ║
╚══════════╧════════╧══════════╝

Options

These options are available on the CLI and as parameters to the Node API.

Name Description
--dep-type Type of dependency to check (dependencies, devDependencies, optionalDependencies, peerDependencies (optional), resolutions) (default: dependencies, devDependencies, optionalDependencies, resolutions) (option can be repeated).
--fix Whether to autofix inconsistencies (using latest version present).
--ignore-dep Dependency to ignore mismatches for (option can be repeated).
--ignore-dep-pattern RegExp of dependency names to ignore mismatches for (option can be repeated).
--ignore-package Workspace package to ignore mismatches for (option can be repeated).
--ignore-package-pattern RegExp of package names to ignore mismatches for (option can be repeated).
--ignore-path Workspace-relative path of packages to ignore mismatches for (option can be repeated).
--ignore-path-pattern RegExp of workspace-relative path of packages to ignore mismatches for (option can be repeated).

Node API

import { CDVC } from 'check-dependency-version-consistency';

const cdvc = new CDVC(path, options);

const result = cdvc.getDependency('eslint');

// Result could look like this:
const result = {
  isFixable: true,
  isMismatching: true,
  name: 'eslint',
  versions: [
    {
      packages: [{ pathRelative: 'packages/package1' }, { pathRelative: 'packages/package2' }],
      version: '^7.0.0',
    },
    {
      packages: [{ pathRelative: 'packages/package3' }],
      version: '^8.0.0',
    },
  ],
};
CDVC Class Constructor Parameter Type Description
path string Path to the workspace root (where the package.json file containing workspaces or pnpm-workspace.yaml is located).
options object See Options.
CDVC Class Member Description
getDependencies() Returns an array of all dependencies in the workspace.
getDependency(name: string) Returns an object with information about an individual dependency.
hasMismatchingDependenciesFixable true if there are any dependencies with mismatching versions that are autofixable.
hasMismatchingDependenciesNotFixable true if there are any dependencies with mismatching versions that are not autofixable.
hasMismatchingDependencies true if there are any dependencies with mismatching versions.
toFixedSummary() Returns a string summary of the mismatching dependency versions that were fixed (if the fix option was specified).
toMismatchSummary() Returns a string of human-readable tables describing the mismatching dependency versions.
Dependency Object Property Description
isFixable true if the mismatching versions of this dependency are autofixable.
isMismatching true if there are multiple versions of this dependency.
name The dependency's name.
versions A list of the versions present of this dependency and the packages each is found in, in the form of: { version: string, packages: { pathRelative: string }[] }.

See lib/cli.ts for an example of how to use it.

References

changelog

check-dependency-version-consistency

v4.1.1 (2024-12-05)

:bug: Bug Fix

:house: Internal

Committers: 1

v4.1.0 (2023-05-31)

:rocket: Enhancement

  • #617 Allow --dep-type option to be specified with CSV (@bmish)

:memo: Documentation

  • #616 Tweak readme example and CLI help text (@bmish)

Committers: 1

v4.0.0 (2023-05-23)

:boom: Breaking Change

  • #609 Use object instead of string to represent package in Node API (@bmish)
  • #608 Consider optionalDependencies by default (@bmish)
  • #607 Drop support for Node 14 and Node 19 (@bmish)
  • #495 Drop support for Node 12 and Node 17 (@ddzz)

:memo: Documentation

  • #611 Show choices in help text for --dep-type (@bmish)

:house: Internal

Committers: 2

v3.3.0 (2023-05-23)

:rocket: Enhancement

  • #605 Add new option --dep-type (@bmish)
  • #606 Add optionalDependencies option to --dep-type (@bmish)
  • #601 Add peerDependencies option to --dep-type (@bmish)

Committers: 1

v3.2.1 (2023-05-20)

:bug: Bug Fix

Committers: 1

v3.2.0 (2023-05-20)

:rocket: Enhancement

:house: Internal

Committers: 1

v3.1.1 (2023-05-11)

:bug: Bug Fix

  • #590 pnpm workspace root package should not require name (@bmish)

Committers: 1

v3.1.0 (2023-05-11)

:rocket: Enhancement

:bug: Bug Fix

  • #586 Add back error messages when unable to detect workspaces (@bmish)

:memo: Documentation

  • #543 Mention competing offering Yarn Constraints in readme (@bmish)
  • #544 Mention npm workspaces in readme (@bmish)

:house: Internal

Committers: 2

v3.0.3 (2022-09-26)

:bug: Bug Fix

Committers: 1

v3.0.2 (2022-09-23)

:bug: Bug Fix

  • #527 Ignore packages inside node_modules from nohoist usage (@bmish)

Committers: 1

v3.0.1 (2022-09-06)

:bug: Bug Fix

  • #520 Allow referring to local package version with workspace: prefix (@bmish)

:house: Internal

Committers: 2

v3.0.0 (2022-03-27)

:boom: Breaking Change

Committers: 3

v2.0.4 (2022-03-18)

:bug: Bug Fix

Committers: 2

v2.0.3 (2022-02-22)

:bug: Bug Fix

  • #358 Autofix to increased latest version range (@bmish)

Committers: 1

v2.0.2 (2022-02-20)

:bug: Bug Fix

  • #355 Maintain range type when fixing to local package version (@bmish)

Committers: 1

v2.0.1 (2022-02-17)

:bug: Bug Fix

  • #347 Improve handling of local package version inconsistencies (@bmish)

Committers: 1

v2.0.0 (2022-02-14)

:boom: Breaking Change

  • #343 Check local package version consistency (@bmish)

:rocket: Enhancement

:memo: Documentation

  • #330 Use latest version instead of highest version (@ddzz)
  • #329 Improve completeness of example in README (@bmish)

Committers: 2

v1.6.0 (2022-01-18)

:rocket: Enhancement

  • #320 Display which dependencies were fixed when running with --fix (@bmish)

:bug: Bug Fix

  • #324 Avoid publishing unnecessary files (@bmish)

:memo: Documentation

  • #315 Add more detailed README example section (@bmish)

:house: Internal

  • #316 Add eslint-plugin-jest and update tests (@ddzz)
  • #313 Update to @types/edit-json-file 1.7.0 and remove ts-ignore comment (@bmish)

Committers: 2

v1.5.0 (2022-01-11)

:rocket: Enhancement

  • #303 Add options --ignore-path and --ignore-path-pattern (@bmish)
  • #301 Add option --ignore-package-pattern (@bmish)
  • #215 Add option --ignore-package and switch to displaying actual package names (@bmish)
  • #298 Add option --ignore-dep-pattern (@bmish)

:bug: Bug Fix

:memo: Documentation

  • #288 Update badge links in README (@ddzz)
  • #285 Update filename formatting in README (@ddzz)

:house: Internal

  • #300 Switch to jest for snapshot testing (@bmish)
  • #296 Add .DS_Store file to .gitignore (@ddzz)

Committers: 3

v1.4.2 (2021-12-08)

:bug: Bug Fix

  • #275 Avoid crash when abnormal version present (@bmish)
  • #271 Correctly expand globs in workspace paths (@bmish)

:house: Internal

  • #253 Add GitHub Actions to Dependabot config (@ddzz)

Committers: 2

v1.4.1 (2021-10-28)

:bug: Bug Fix

  • #241 Don't ignore workspace root package.json (@bmish)
  • #242 Maintain newline at end of package.json file (@bmish)

:house: Internal

Committers: 1

v1.4.0 (2021-10-12)

:rocket: Enhancement

  • #206 Bold maximum usage count for each mismatched dependency (@bmish)
  • #205 Improve discoverability of fix option (@bmish)

:bug: Bug Fix

  • #238 Fix autofixer when dots are present in dependency name (@bmish)

:house: Internal

Committers: 2

v1.3.0 (2021-09-09)

:rocket: Enhancement

  • #198 Show list of packages containing each dependency version (@bmish)
  • #197 Use table to display mismatching version output (@bmish)
  • #189 Display dependency versions in descending instead of ascending order (@ddzz)

Committers: 2

v1.2.0 (2021-09-07)

:rocket: Enhancement

  • #194 Make path argument optional (@ddzz)
  • #185 Improve output formatting for mismatched versions (@ddzz)

:bug: Bug Fix

  • #184 Remove clutter from error message output (@ddzz)

:memo: Documentation

Committers: 2

v1.1.0 (2021-09-04)

:rocket: Enhancement

  • #177 Implement autofixing (@bmish)
  • #178 Sort mismatching version output by version number (@bmish)
  • #174 Add help description to CLI path argument (@bmish)

:bug: Bug Fix

:memo: Documentation

  • #175 Mention related tool npm-package-json-lint in README (@bmish)

Committers: 1

v1.0.0 (2021-08-31)

:boom: Breaking Change

:rocket: Enhancement

:bug: Bug Fix

Committers: 1

v0.3.0 (2021-05-08)

:rocket: Enhancement

  • #88 Exit with failure when unnecessarily specifying --ignore-dep CLI option (@bmish)

:bug: Bug Fix

  • #95 Add real package.json types from type-fest (@bmish)

Committers: 1

v0.2.0 (2021-03-13)

:rocket: Enhancement

  • #59 Show count of each mismatching version for a dependency (@bmish)

Committers: 1

0.1.1 (2020-12-28)

:bug: Bug Fix

  • #2 Remove unused js-yaml dependency (@bmish)

:memo: Documentation

  • #6 Add MIT license (@bmish)
  • #5 Add some package.json keywords (@bmish)
  • #4 Clarify a few aspects of the README (@bmish)

Committers: 1

0.1.0 (2020-12-25)

  • Initial version