Value Groups
Value groups keep values in package.json files which are not versions, such as author, license, repository or scripts, the same across your packages. Each group selects packages by name and gives them values. They are checked by format --check and written by format.
Examples
{ "valueGroups": [ { "label": "Every package has the same author, license and build script, and no prepublish script", "values": { "author": "Jo Bloggs <jo@example.com>", "license": "MIT", "scripts": { "build": "tsc", "prepublish": null } } }, { "label": "Apps build with Vite instead, and are never published", "packages": ["@my-repo/app-*"], "values": { "private": true, "scripts.build": "vite build" } } ]}Any other scripts each package has are left alone.
How groups are applied
Unlike other groups, a package is not limited to the first value group it matches. Every value group which matches a package is applied to it, in the order they are written. When two groups give a value to the same property, the group written last wins.
This means you can write broad defaults first and follow them with more specific groups for some packages.
A package without a name is never given values by a value group.
Values are expected as the rest of format would leave them: when sortExports, sortAz, formatBugs or formatRepository would change a value, the changed value is the one expected.
Properties
label Optional
A short name or description of the group, for your own reference.
packages Optional
- An array of strings which should match the
nameproperties of your package.json files. - If omitted, the default behaviour is to match every package.
- Negated names are also supported, so a value of
["!my-client", "!my-server"]would assign everything except the packagesmy-clientandmy-serverto this group. - The strings can be any combination of exact matches or glob patterns:
// ✅ match any package namepackages: ["**"]
// ✅ match any package name with this scopepackages: ["@my-repo/**"]
// ✅ match specific packages by namepackages: ["my-server", "my-client"]
// ✅ match all packages except negated onespackages: ["!my-server", "!@my-repo/**"]
// ❌ no mixing of specific and negated packagespackages: ["my-client", "!@my-repo/**"]
// ❌ not file system paths, name properties of package.json filespackages: ["packages/my-client"]
// ❌ not file system globs, name properties of package.json filespackages: ["packages/**"]{ "name": "HERE", "version": "1.0.2"}strategy Optional
How the group gives each of its values to a package. Defaults to "merge".
"merge"deeply merges each value into what the package has. Objects are merged key by key, so any keys the group does not mention are left alone. Arrays, strings, numbers and booleans replace what was there."replace"makes each value exactly what the package has, removing any keys of an object which the group does not mention. Each removed key is reported asPropertyIsNotInValueGroup, andseveritydecides whetherformat --checktreats it as an error or a warning. The keys of an object are also put in the order the group writes them, unlessformatsorts them afterwards.
{ "scripts": { "build": "babel", "lint": "eslint" }}{ "values": { "scripts": { "build": "tsc" } }}{ "scripts": { "build": "tsc", "lint": "eslint" }}{ "strategy": "replace", "values": { "scripts": { "build": "tsc" } }}{ "scripts": { "build": "tsc" }}severity Optional
How format --check treats a property which a "replace" group removes because it does not list it. format removes the property whatever its severity.
{ "valueGroups": [ { "label": "Remove scripts we do not use, but do not fail CI over them", "strategy": "replace", "severity": { "PropertyIsNotInValueGroup": "warn" }, "values": { "scripts": { "build": "tsc", "test": "vitest" } } } ]}| Value | Effect on format --check |
|---|---|
"fix" | Default. Show as error; exits 1. |
"warn" | Show as warning; does not affect exit code. |
"error" | Show as error; exits 1. |
strict does not change it. When more than one value group removes the property, the severity of the last of them is used.
Permitted keys:
values Required
An object of the values each matched package must have, written as you would write them in package.json.
- A value of
nullmeans the property must not exist, andformatwill remove it. When that leaves an object with nothing in it, such asscriptsonce its only script is removed, the empty object is removed too. An object which was already empty is left alone. - Merging an object into a property which is not an object, such as
{ "repository": { "directory": "packages/a" } }into"repository": "User/repo", replaces it. - A key of
valuescan be a dotted path to a nested property, such as"scripts.build". This lets a later group change one nested property without repeating the rest. - To use a dot within a key, write a backslash before it. In JSON a backslash is itself written as
\\, so"scripts.build\\.esm"means thebuild.esmproperty ofscripts. A backslash within a key is written\\\\. - Only the keys of
valuesitself are dotted paths. Keys of the objects within it are used exactly as written, so{ "exports": { "./utils.js": "./dist/utils.js" } }needs no backslashes.
// ✅ the author property"values": { "author": "Jo Bloggs"}
// ✅ the build script, leaving other scripts alone"values": { "scripts.build": "tsc"}
// ✅ the same thing, written as an object"values": { "scripts": { "build": "tsc" }}
// ✅ no "private" property"values": { "private": null}
// ✅ the "build.esm" script"values": { "scripts.build\\.esm": "tsc -p tsconfig.esm.json"}
// ❌ an empty key"values": { "scripts..build": "tsc"}
// ❌ a backslash not followed by "." or "\""values": { "scripts.build\\esm": "tsc"}
// ❌ an empty key within a value"values": { "exports": { "": "./index.js" }}Status codes
When a package does not have the values its value groups give it, format --check reports one of these for each property, with the value it has and the value it should have:
| Status code | Meaning |
|---|---|
PropertyDiffersFromValueGroup | The property is missing or does not have the value it is given. |
PropertyIsBannedByValueGroup | The property exists, but a value group gives it the value null. |
PropertyIsNotInValueGroup | The property is within an object a "replace" group gives, but the group does not list it. |
Config errors
These mistakes in valueGroups stop syncpack with an error instead of being ignored:
- A group without
values, or with an emptyvaluesobject. - A key of
values, or of any object within a value, which is an empty string, or a dotted path with an empty key such as"scripts..build". - A backslash in a key of
valueswhich is not followed by.or\. - A key of
severityother thanPropertyIsNotInValueGroup. - A property other than
label,packages,severity,strategyandvalues. Value groups select packages by name, sodependencies,dependencyTypes,specifierTypesandisIgnoredare not supported. Use a negated pattern inpackages, such as["!my-package"], to leave a package out.