eslint-plugin-package-json

Rules for consistent, readable, and valid package.json files. 🗂️

MIT License

Downloads
136K
Stars
84
Committers
17

Installation

This package requires ESLint 8 and jsonc-eslint-parser:

npm install eslint eslint-plugin-package-json jsonc-eslint-parser --save-dev

Usage

Flat Config

This plugin's recommended configuration enables its rules on **/package.json files, parsing them with jsonc-eslint-parser:

In your ESLint configuration file:

import packageJson from "eslint-plugin-package-json/configs/recommended";

export default [
	// your other ESLint configurations
	packageJson,
];

If you want to override the recommended rules:

import packageJson from "eslint-plugin-package-json/configs/recommended";

export default [
	// your other ESLint configurations
	{
		...packageJson,
		rules: {
			...packageJson.rules,
			"package-json/valid-package-def": "off",
		},
	},
];

See ESLint's Configuration Files guide for details on how to customize your rules and other config settings.

Legacy Config

Add an override to your ESLint configuration file that specifies this plugin, jsonc-eslint-parser, and its recommended rules for your package.json file:

module.exports = {
	overrides: [
		{
			extends: ["plugin:package-json/recommended"],
			files: ["package.json"],
			parser: "jsonc-eslint-parser",
			plugins: ["package-json"],
		},
	],
};

You may also want to individually configure rules. See ESLint's Configure Rules guide for details on how to customize your rules.

module.exports = {
	overrides: [
		{
			extends: ["plugin:package-json/recommended"],
			files: ["package.json"],
			parser: "jsonc-eslint-parser",
			plugins: ["package-json"],
			rules: {
				"package-json/valid-package-def": "error",
			},
		},
	],
};

Usage Alongside Prettier

prettier-plugin-packagejson is a Prettier plugin that enforces the same package.json keys ordering as the order-properties and sort-collections rules with default options. We recommend using both the Prettier plugin and eslint-plugin-package-json's recommended configuration. The default settings don't conflict, and Prettier plugins can quickly fix up ordering in your editor on save and/or as a Git hook.

Supported Rules

💼 Configurations enabled in. ✅ Set in the recommended configuration. 🔧 Automatically fixable by the --fix CLI option. 💡 Manually fixable by editor suggestions.

Name                       Description 💼 🔧 💡
order-properties Package properties must be declared in standard order 🔧
repository-shorthand Enforce either object or shorthand declaration for repository. 🔧
sort-collections Dependencies, scripts, and configuration values must be declared in alphabetical order. 🔧
unique-dependencies Enforce that if repository directory is specified, it matches the path to the package.json file 💡
valid-local-dependency Checks existence of local dependencies in the package.json
valid-name Enforce that package names are valid npm package names
valid-package-def Enforce that package.json has all properties required by the npm spec
valid-repository-directory Enforce that if repository directory is specified, it matches the path to the package.json file 💡
valid-version Enforce that package versions are valid semver specifiers

These rules only run on package.json files; they will ignore all other files being linted. They can lint package.json files at project root and in any subfolder of the project, making this plugin great for monorepos.

Contributors

Appreciation

Many thanks to @zetlen for creating the initial version and core infrastructure of this package! 💖

💙 This package was templated with create-typescript-app.

Package Rankings
Top 2.61% on Npmjs.org