Skip to content

useSortedPackageJson (JSON)

Language JSON (and super languages)
.vscode/settings.json
{
"editor.codeActionsOnSave": {
"source.action.useSortedPackageJson.biome": "explicit",
"source.fixAll.biome": "explicit"
}
}
biome.json
{
"assist": {
"actions": {
"source": {
"useSortedPackageJson": "on"
}
}
}
}

Organize package.json fields according to established conventions.

The action follows the conventions of the sort-package-json tool.

Known fields follow the order in the table below. Unlisted fields are sorted alphabetically after known fields, while fields beginning with _ are placed last. “Key sort” means sorting the keys of a nested object. “Unique items” removes repeated array entries while preserving their order; “sort unique items” also sorts the remaining entries.

KeyRules
$schema
name
displayName
version
stableVersion
private
description
categoriesUnique items
keywordsUnique items
homepage
bugsKey order: url, email
repositoryKey order: type, url
fundingKey order: type, url
licenseKey order: type, url
qna
authorKey order: name, email, url
maintainersKey order (per item): name, email, url
contributorsKey order (per item): name, email, url
publisher
sideEffects
type
imports
exports
main
svelte
umd:main
jsdelivr
unpkg
module
source
jsnext:main
browser
react-native
types
typesVersions
typings
style
example
examplestyle
assets
binKey sort
man
directoriesKey order: lib, bin, man, doc, example, test
filesUnique items
workspacesKey order (when object): packages, catalog
binaryKey order: module_name, module_path, remote_path, package_name, host
scriptsScript sort
betterScriptsScript sort
l10n
contributesKey sort
activationEventsUnique items
huskySorts the hooks field using git hook sort
simple-git-hooksKey sort using git hook sort
pre-commit
commitlintKey sort
lint-staged
nano-staged
configKey sort
nodemonConfigKey sort
browserifyKey sort
babelKey sort
browserslist
xoKey sort
prettierPrettier sort
eslintConfigESLint sort
eslintIgnore
npmpackagejsonlintKey sort (also recognizes: npmPackageJsonLintConfig, npmpkgjsonlint)
releaseKey sort
remarkConfigKey sort
stylelint
avaKey sort
jestKey sort
jest-junitKey sort
jest-stareKey sort
mochaKey sort
nycKey sort
c8Key sort
tapKey sort
oclifKey sort (deep)
resolutionsKey sort
dependenciesKey sort
devDependenciesKey sort
dependenciesMetaKey sort (deep)
peerDependenciesKey sort
peerDependenciesMetaKey sort (deep)
optionalDependenciesKey sort
bundledDependenciesSort unique items
bundleDependenciesSort unique items
extensionPackSort unique items
extensionDependenciesSort unique items
flat
packageManager
enginesKey sort
engineStrictKey sort
devEnginesKey order (packageManager): name, version, onFail
voltaKey order: node, npm, yarn
languageName
os
cpu
preferGlobalKey sort
publishConfigKey sort
icon
badgesKey order (per item): description, url, href
galleryBannerKey sort
preview
markdown
pnpmBase property order: neverBuiltDependencies, onlyBuiltDependencies, onlyBuiltDependenciesFile, overrides, packageExtensions, patchedDependencies, peerDependencyRules, allowedDeprecatedVersions, allowNonAppliedPatches, auditConfig, ignoredOptionalDependencies, updateConfig. Other properties sorted alphabetically. Deep sorting applied.

Fields are sorted by the order they are listed below:

KeyRules
env
parser
parserOptions
settings
plugins
extends
rulesGroup built-in rules first, then plugin rules. Each group is sorted
overridesKey order (per item): files, excludedFiles
globals
processor
noInlineConfig
reportUnusedDisableDirectives

Item order:

  • applypatch-msg
  • pre-applypatch
  • post-applypatch
  • pre-commit
  • pre-merge-commit
  • prepare-commit-msg
  • commit-msg
  • post-commit
  • pre-rebase
  • post-checkout
  • post-merge
  • pre-push
  • pre-receive
  • update
  • proc-receive
  • post-receive
  • post-update
  • reference-transaction
  • push-to-checkout
  • pre-auto-gc
  • post-rewrite
  • sendemail-validate
  • fsmonitor-watchman
  • p4-changelist
  • p4-prepare-changelist
  • p4-post-changelist
  • p4-pre-submit
  • post-index-change

Keys are sorted alphabetically except for overrides, which is placed last. Keys inside overrides entries and their options objects are also sorted.

Keys are sorted alphabetically except for pre- and post-scripts, which are placed immediately before and after their base script.

For example, preinstall and postinstall stay next to install:

{
"scripts": {
"build": "",
"preinstall": "",
"install": "",
"postinstall": "",
"lint": ""
}
}

Scripts for which the pre/post order is applied:

  • install
  • pack
  • prepare
  • publish
  • restart
  • shrinkwrap
  • start
  • stop
  • test
  • uninstall
  • version
package.json
{
"dependencies": {
"lodash": "^4.0.0"
},
"name": "my-package",
"version": "1.0.0"
}
/package.json ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ Source action diff:

1 1 │ {
2 │ - ··"dependencies":·{
3 │ - ····"lodash":·"^4.0.0"
4 │ - ··},
5 │ - ··"name":·"my-package",
6 │ - ··"version":·"1.0.0"
2 │ + ··"name":·"my-package",
3 │ + ··"version":·"1.0.0",
4 │ + ··"dependencies":·{
5 │ + ····"lodash":·"^4.0.0"
6 │ + ··}
7 7 │ }
8 8 │

package.json
{
"name": "my-package",
"version": "1.0.0",
"dependencies": {
"lodash": "^4.0.0"
}
}