syncpack
Manage multiple package.json files, such as in Lerna Monorepos and Yarn/Pnpm
Workspaces
![Maintainability](https://api.codeclimate.com/v1/badges/516439365fdd0e3c6526/maintainability)
🌩 Installation
npm install --global syncpack
📝 Commands
fix-mismatches
Ensure that multiple packages requiring the same dependency define the same
version, so that every package requires eg. react@16.4.2
, instead of a
combination of react@16.4.2
, react@0.15.9
, and react@16.0.0
.
See versionGroups
if you have advanced requirements.
Options
-s, --source [pattern] glob pattern for package.json files to read from
-f, --filter [pattern] only include dependencies whose name matches this regex
-p, --prod include dependencies
-d, --dev include devDependencies
-P, --peer include peerDependencies
-R, --resolutions include resolutions (yarn)
-o, --overrides include overrides (pnpm)
-w, --workspace include locally developed package versions
-i, --indent [value] override indentation. defaults to " "
-h, --help display help for command
Examples
syncpack fix-mismatches
syncpack fix-mismatches --source "apps/*/package.json"
syncpack fix-mismatches --source "apps/*/package.json" --source "core/*/package.json"
syncpack fix-mismatches --filter "typescript|tslint"
syncpack fix-mismatches --dev
syncpack fix-mismatches --dev --peer
syncpack fix-mismatches --indent " "
format
Organise package.json files according to a conventional format, where fields
appear in a predictable order and nested fields are ordered alphabetically.
Shorthand properties are used where available, such as the "repository"
and
"bugs"
fields.
Options
-s, --source [pattern] glob pattern for package.json files to read from
-i, --indent [value] override indentation. defaults to " "
-h, --help output usage information
Examples
syncpack format
syncpack format --source "apps/*/package.json"
syncpack format --source "apps/*/package.json" --source "core/*/package.json"
syncpack format --indent " "
lint-semver-ranges
Check whether dependency versions used within "dependencies", "devDependencies",
and "peerDependencies" follow a consistent format.
See semverGroups
if you have advanced requirements.
Options
-s, --source [pattern] glob pattern for package.json files to read from
-f, --filter [pattern] only include dependencies whose name matches this regex
-r, --semver-range <range> see supported ranges below. defaults to ""
-p, --prod include dependencies
-d, --dev include devDependencies
-P, --peer include peerDependencies
-R, --resolutions include resolutions (yarn)
-o, --overrides include overrides (pnpm)
-w, --workspace include locally developed package versions
-h, --help display help for command
Examples
syncpack lint-semver-ranges
syncpack lint-semver-ranges --source "apps/*/package.json"
syncpack lint-semver-ranges --source "apps/*/package.json" --source "core/*/package.json"
syncpack lint-semver-ranges --filter "typescript|tslint"
syncpack lint-semver-ranges --semver-range ~
syncpack lint-semver-ranges --dev --semver-range ~
syncpack lint-semver-ranges --dev --peer --semver-range ~
list
List all dependencies required by your packages.
Options
-s, --source [pattern] glob pattern for package.json files to read from
-f, --filter [pattern] only include dependencies whose name matches this regex
-p, --prod include dependencies
-d, --dev include devDependencies
-P, --peer include peerDependencies
-R, --resolutions include resolutions (yarn)
-o, --overrides include overrides (pnpm)
-w, --workspace include locally developed package versions
-h, --help display help for command
Examples
syncpack list
syncpack list --source "apps/*/package.json"
syncpack list --source "apps/*/package.json" --source "core/*/package.json"
syncpack list --filter "typescript|tslint"
syncpack list --dev
syncpack list --dev --peer
list-mismatches
List dependencies which are required by multiple packages, where the version is
not the same across every package.
See versionGroups
if you have advanced requirements.
Options
-s, --source [pattern] glob pattern for package.json files to read from
-f, --filter [pattern] only include dependencies whose name matches this regex
-p, --prod include dependencies
-d, --dev include devDependencies
-P, --peer include peerDependencies
-R, --resolutions include resolutions (yarn)
-o, --overrides include overrides (pnpm)
-w, --workspace include locally developed package versions
-h, --help display help for command
Examples
syncpack list-mismatches
syncpack list-mismatches --source "apps/*/package.json"
syncpack list-mismatches --source "apps/*/package.json" --source "core/*/package.json"
syncpack list-mismatches --filter "typescript|tslint"
syncpack list-mismatches --dev
syncpack list-mismatches --dev --peer
set-semver-ranges
Ensure dependency versions used within "dependencies"
, "devDependencies"
,
and "peerDependencies"
follow a consistent format.
See semverGroups
if you have advanced requirements.
Options
-s, --source [pattern] glob pattern for package.json files to read from
-r, --semver-range <range> see supported ranges below. defaults to ""
-f, --filter [pattern] only include dependencies whose name matches this regex
-p, --prod include dependencies
-d, --dev include devDependencies
-P, --peer include peerDependencies
-R, --resolutions include resolutions (yarn)
-o, --overrides include overrides (pnpm)
-w, --workspace include locally developed package versions
-i, --indent [value] override indentation. defaults to " "
-h, --help display help for command
Examples
syncpack set-semver-ranges
syncpack set-semver-ranges --source "apps/*/package.json"
syncpack set-semver-ranges --source "apps/*/package.json" --source "core/*/package.json"
syncpack set-semver-ranges --filter "typescript|tslint"
syncpack set-semver-ranges --semver-range ~
syncpack set-semver-ranges --dev --semver-range ~
syncpack set-semver-ranges --dev --peer --semver-range ~
syncpack set-semver-ranges --indent " "
🛠 Configuration File
Creating a configuration file is optional, syncpack will search up the directory
tree in the following places:
- a
syncpack
property in package.json
- a
.syncpackrc
file in JSON or YAML format - a
.syncpackrc.json
, .syncpackrc.yaml
, .syncpackrc.yml
, .syncpackrc.js
,
or .syncpackrc.cjs
file - a
syncpack.config.js
or syncpack.config.cjs
CommonJS module exporting an
object
Default Configuration
{
"dev": true,
"filter": ".",
"indent": " ",
"overrides": true,
"peer": true,
"prod": true,
"resolutions": true,
"workspace": true,
"semverGroups": [],
"semverRange": "",
"sortAz": [
"contributors",
"dependencies",
"devDependencies",
"keywords",
"peerDependencies",
"resolutions",
"scripts"
],
"sortFirst": [
"name",
"description",
"version",
"author"
],
"source": [],
"versionGroups": []
}
dev
, peer
, prod
, resolutions
, overrides
, and workspace
Whether to search within devDependencies
, peerDependencies
, dependencies
,
resolutions
(Yarn), overrides
(Pnpm), and the version
property of the
package.json files of your own packages developed within your workspace
respectively.
All of these locations are searched by default but they can be disabled
individually in your config file. If any are set via the command line options
--dev
, --peer
, --prod
, --resolutions
, --overrides
, or --workspace
then only the options you provide will be searched.
filter
A string which will be passed to new RegExp()
to match against package names
that should be included.
indent
The character(s) to be used to indent your package.json files when writing to
disk.
semverRange
Defaulted to ""
to ensure that exact dependency versions are used instead of
loose ranges, but this can be overridden in your config file or via the
--semver-range
command line option.
Supported Ranges
< <1.4.2
<= <=1.4.2
"" 1.4.2
~ ~1.4.2
^ ^1.4.2
>= >=1.4.2
> >1.4.2
* *
sortAz
When using the format
command, determines which fields within package.json
files should be sorted alphabetically. When the value is an Object, its keys are
sorted alphabetically. When the value is an Array, its values are sorted
alphabetically. There is no equivalent CLI Option for this configuration.
sortFirst
When using the format
command, determines which fields within package.json
files should appear at the top, and in what order. There is no equivalent CLI
Option for this configuration.
source
Defaults to ["package.json", "packages/*/package.json"]
to match most Projects
using Lerna or Yarn Workspaces, but this can be overridden in your config file
or via multiple --source
command line options. Supports any patterns supported
by glob.
versionGroups
The most common use case for version groups is when some of the packages in your
Monorepo are considered alpha (or legacy). Since those packages are much further
ahead (or behind) the other packages, the dependencies within those packages
need to be managed differently to the rest of the Monorepo.
Your alpha packages might use unstable versions of some dependencies, while the
rest of the repo might need to remain on stable versions.
You don't want mismatches within your alpha packages, you don't want mismatches
within the other packages, but you do want those groups to use different
versions to each other and not have syncpack
make them all the same.
In the following example, 2 of our packages are using different versions of
react
and react-dom
to the rest of the project.
{
"versionGroups": [
{
"dependencies": ["react", "react-dom"],
"packages": ["@alpha/server", "@alpha/ui"]
}
]
}
👋 The dependencies
and packages
fields are processed using
minimatch, so the above example can
also be written as "packages": ["@alpha/**"]
.
syncpack
will make ensure that:
- The versions of
react
and react-dom
are the same within @alpha/server
and @alpha/ui
. - The versions of
react
and react-dom
are the same across every package
except @alpha/server
and @alpha/ui
. - The versions of
react
and react-dom
within @alpha/server
and @alpha/ui
can be different to the other packages in the monorepo. - The versions of every other dependency in the monorepo (eg
lodash
) are the
same across every package including @alpha/server
and @alpha/ui
.
Each dependency can only belong to one version group, the first rule which
matches a given dependency and package will apply.
You can be quite granular with these rules, so the partitioning doesn't have
to apply to an entire package:
- A specific dependency in a specific package.
- A specific dependency in some specific packages only.
- Any dependency who name matches a pattern such as
@aws-sdk/**
.
See semverGroups
for some examples, they work the same way.
semverGroups
Allow some packages to have different semver range rules to the rest of your
monorepo. Each dependency can only belong to one semver group, the first rule
which matches a given dependency and package will apply.
Example use cases
1: Every dependency of @myrepo/library
should have a semver range of ~
,
regardless of what the rest of the monorepo uses:
{
"semverGroups": [
{
"range": "~",
"dependencies": ["**"],
"packages": ["@myrepo/library"]
}
]
}
2: Every dependency of @myrepo/library
whose name matches @alpha/**
should
have a semver range of ^
, regardless of what the rest of that package or the
rest of the monorepo uses:
{
"semverGroups": [
{
"range": "^",
"dependencies": ["@alpha/**"],
"packages": ["@myrepo/library"]
}
]
}
3: Every dependency in the monorepo whose name matches @alpha/**
should have a
semver range of ~
, regardless of what the rest of the monorepo uses:
{
"semverGroups": [
{
"range": "~",
"dependencies": ["@alpha/**"],
"packages": ["**"]
}
]
}
🕵🏾♀️ Resolving Packages
package.json files are resolved in this order of precendence:
- If
--source
glob patterns are
provided, use those. - If using Yarn Workspaces,
read
workspaces
from ./package.json
. - If using Lerna, read
packages
from
./lerna.json
. - If using Pnpm, read
packages
from
./pnpm-workspace.yaml
. - Default to
'package.json'
and 'packages/*/package.json'
.
🙋🏿♀️ Getting Help
Get help with issues by creating a Bug Report or discuss ideas by opening a
Feature Request.
👀 Other Projects
If you find my Open Source projects useful, please share them ❤️