Rules for consistent, readable, and valid package.json files. ποΈ
Rules for consistent, readable, and valid package.json files.
ποΈ
This package requires ESLint >=8:
``shell`
npm install eslint eslint-plugin-package-json --save-dev
This plugin's recommended configuration enables its rules on **/package.json files, parsing them with jsonc-eslint-parser.
`ts
// eslint.config.ts
import packageJson from "eslint-plugin-package-json";
import { defineConfig } from "eslint/config";
export default defineConfig([
// your other ESLint configurations
packageJson.configs.recommended,
]);
`
If you want to override the recommended rules:
`ts
// eslint.config.ts
import packageJson from "eslint-plugin-package-json";
import { defineConfig } from "eslint/config";
export default defineConfig([
// your other ESLint configurations
{
extends: [packageJson.configs.recommended],
files: ["package.json"],
rules: {
"package-json/require-author": "error",
},
},
]);
`
See ESLint's _Configuration Files_ guide for details on how to customize your rules and other config settings.
The recommended-publishable configuration has everything in it from the standard recommended config, with some additional rules added that are geared towards packages that are intended to be published.
`ts
// eslint.config.ts
import packageJson from "eslint-plugin-package-json";
import { defineConfig } from "eslint/config";
export default defineConfig([
// your other ESLint configurations
packageJson.configs["recommended-publishable"],
]);
`
The stylistic configuration sets up the parser and files similar to the recommended config, but includes rules that are more opinionated about the style of a package.json.
This can be used in addition to the recommended config, or on its own.
`ts
// eslint.config.ts
import packageJson from "eslint-plugin-package-json";
import { defineConfig } from "eslint/config";
export default defineConfig([
// your other ESLint configurations
packageJson.configs.recommended, // or packageJson.configs["recommended-publishable"]
packageJson.configs.stylistic,
]);
`
Usage with ESLint's legacy ("eslintrc") format requires also installing jsonc-eslint-parser:
`shell`
npm install jsonc-eslint-parser --save-dev
Add an override to your ESLint configuration file that specifies jsonc-eslint-parser, this plugin, and its recommended rules for your package.json file:
`ts`
module.exports = {
overrides: [
{
extends: ["plugin:package-json/legacy-recommended"],
files: ["package.json"],
parser: "jsonc-eslint-parser",
},
],
};
You may also want to individually configure rules.
See ESLint's _Configure Rules_ guide for details on how to customize your rules.
`ts`
module.exports = {
overrides: [
{
extends: ["plugin:package-json/legacy-recommended"],
files: ["package.json"],
parser: "jsonc-eslint-parser",
rules: {
"package-json/valid-package-definition": "error",
},
},
],
};
Some rules can be configured in ESLint shared settings.
You can set them in settings.packageJson in an ESLint flat config.
Example:
`ts
// eslint.config.ts
import packageJson from "eslint-plugin-package-json";
import { defineConfig } from "eslint/config";
export default defineConfig({
plugins: {
"package-json": packageJson,
},
rules: {
// description won't be required in package.json with "private": true`
"package-json/require-description": "error",
},
settings: {
packageJson: {
enforceForPrivate: false,
},
},
});
#### enforceForPrivate
- Type: boolean
- Default: [see below]
When a package.json file has a "private": true field, it indicates that the package will not be published to npm (or another online registry).require-*
Some fields that are nice to have in public packages become less relevant when a package is private.
This option determines whether rules, if used, should enforce the presence of the corresponding property in package.json files that have "private": true.
By default, this is:
- false for require-name and require-version.true
- for every other require-* rule.
By specifying this setting as true or false, it will override the defaults and apply the setting for ALL rules.require-
In that case, either all rules will be applied to private packages or no require- rules will be applied to private packages.ignorePrivate
Even then, you can override the setting again at the rule level, by using the rule's option, which will take precedence over this global setting.
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.eslint-plugin-package-json
We recommend using both the Prettier plugin and '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.
πΌ Configurations enabled in.\
βοΈ Set in the legacy-recommended configuration.\recommended
β
Set in the configuration.\recommended-publishable
π¦ Set in the configuration.\stylistic
π¨ Set in the configuration.\--fix
π§ Automatically fixable by the CLI option.\
π‘ Manually fixable by editor suggestions.\
β Deprecated.
| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | Description | πΌ | π§ | π‘ | β |
| :------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------ | :- | :- | :- |
| bin-name-casing | Enforce that names for bin properties are in kebab case. | π¨ | | π‘ | |
| exports-subpaths-style | Enforce consistent format for the exports field (implicit or explicit subpaths). | π¨ | π§ | | |
| no-empty-fields | Reports on unnecessary empty arrays and objects. | βοΈ β
π¦ | | π‘ | |
| no-redundant-files | Prevents adding unnecessary / redundant files. | βοΈ β
π¦ | | π‘ | |
| no-redundant-publishConfig | Warns when publishConfig.access is used in unscoped packages. | βοΈ β
π¦ | | π‘ | |
| order-properties | Package properties should be declared in standard order | π¨ | π§ | | |
| repository-shorthand | Enforce either object or shorthand declaration for repository. | βοΈ β
π¦ | π§ | | |
| require-attribution | Ensures that proper attribution is included, requiring that either author or contributors is defined, and that if contributors is present, it should include at least one contributor. | π¦ | | π‘ | |author
| require-author | Requires the property to be present. | | | | |bugs
| require-bugs | Requires the property to be present. | | | | |bundleDependencies
| require-bundleDependencies | Requires the property to be present. | | | | |dependencies
| require-dependencies | Requires the property to be present. | | | | |description
| require-description | Requires the property to be present. | βοΈ β
π¦ | | | |devDependencies
| require-devDependencies | Requires the property to be present. | | | | |engines
| require-engines | Requires the property to be present. | | | | |exports
| require-exports | Requires the property to be present. | π¦ | | | |files
| require-files | Requires the property to be present. | π¦ | | | |homepage
| require-homepage | Requires the property to be present. | | | | |keywords
| require-keywords | Requires the property to be present. | | | | |license
| require-license | Requires the property to be present. | βοΈ β
π¦ | | | |name
| require-name | Requires the property to be present. | βοΈ β
π¦ | | | |optionalDependencies
| require-optionalDependencies | Requires the property to be present. | | | | |peerDependencies
| require-peerDependencies | Requires the property to be present. | | | | |repository
| require-repository | Requires the property to be present. | π¦ | | | |scripts
| require-scripts | Requires the property to be present. | | | | |sideEffects
| require-sideEffects | Requires the property to be present. | π¦ | | | |type
| require-type | Requires the property to be present. | βοΈ β
π¦ | π§ | | |types
| require-types | Requires the property to be present. | | | | |version
| require-version | Requires the property to be present. | βοΈ β
π¦ | | | |scripts
| restrict-dependency-ranges | Restricts the range of dependencies to allow or disallow specific types of ranges. | | | π‘ | |
| restrict-private-properties | Disallows unnecessary properties in private packages. | | π§ | π‘ | |
| scripts-name-casing | Enforce that names for are in kebab case (optionally separated by colons). | π¨ | | π‘ | |dependencies
| sort-collections | Selected collections must be in a consistent order (lexicographical for most; lifecycle-aware for scripts). | βοΈ β
π¦ | π§ | | |
| specify-peers-locally | Requires that all peer dependencies are also declared as dev dependencies | βοΈ β
π¦ | | π‘ | |
| unique-dependencies | Checks a dependency isn't specified more than once (i.e. in and devDependencies) | βοΈ β
π¦ | | π‘ | |author
| valid-author | Enforce that the property is valid. | βοΈ β
π¦ | | | |bin
| valid-bin | Enforce that the property is valid. | βοΈ β
π¦ | | | |bundleDependencies
| valid-bundleDependencies | Enforce that the (also: bundledDependencies) property is valid. | βοΈ β
π¦ | | | |config
| valid-config | Enforce that the property is valid. | βοΈ β
π¦ | | | |contributors
| valid-contributors | Enforce that the property is valid. | βοΈ β
π¦ | | | |cpu
| valid-cpu | Enforce that the property is valid. | βοΈ β
π¦ | | | |dependencies
| valid-dependencies | Enforce that the property is valid. | βοΈ β
π¦ | | | |description
| valid-description | Enforce that the property is valid. | βοΈ β
π¦ | | | |devDependencies
| valid-devDependencies | Enforce that the property is valid. | βοΈ β
π¦ | | | |directories
| valid-directories | Enforce that the property is valid. | βοΈ β
π¦ | | | |engines
| valid-engines | Enforce that the property is valid. | βοΈ β
π¦ | | | |exports
| valid-exports | Enforce that the property is valid. | βοΈ β
π¦ | | | |files
| valid-files | Enforce that the property is valid. | βοΈ β
π¦ | | | |homepage
| valid-homepage | Enforce that the property is valid. | βοΈ β
π¦ | | | |keywords
| valid-keywords | Enforce that the property is valid. | βοΈ β
π¦ | | | |license
| valid-license | Enforce that the property is valid. | βοΈ β
π¦ | | | |main
| valid-main | Enforce that the property is valid. | βοΈ β
π¦ | | | |man
| valid-man | Enforce that the property is valid. | βοΈ β
π¦ | | | |module
| valid-module | Enforce that the property is valid. | βοΈ β
π¦ | | | |optionalDependencies
| valid-name | Enforce that package names are valid npm package names | βοΈ β
π¦ | | | |
| valid-optionalDependencies | Enforce that the property is valid. | βοΈ β
π¦ | | | |os
| valid-os | Enforce that the property is valid. | βοΈ β
π¦ | | | |peerDependencies
| valid-package-definition | Enforce that package.json has all properties required by the npm spec | | | | β |
| valid-peerDependencies | Enforce that the property is valid. | βοΈ β
π¦ | | | |private
| valid-private | Enforce that the property is valid. | βοΈ β
π¦ | | | |publishConfig
| valid-publishConfig | Enforce that the property is valid. | βοΈ β
π¦ | | | |repository
| valid-repository | Enforce that the property is valid. | βοΈ β
π¦ | | | |scripts
| valid-repository-directory | Enforce that if repository directory is specified, it matches the path to the package.json file | βοΈ β
π¦ | | π‘ | |
| valid-scripts | Enforce that the property is valid. | βοΈ β
π¦ | | | |sideEffects
| valid-sideEffects | Enforce that the property is valid. | βοΈ β
π¦ | | | |type
| valid-type | Enforce that the property is valid. | βοΈ β
π¦ | | | |workspaces
| valid-version | Enforce that package versions are valid semver specifiers | βοΈ β
π¦ | | | |
| valid-workspaces | Enforce that the property is valid. | βοΈ β
π¦ | | | |
These rules only run on package.json files; they will ignore all other files being linted.package.json
They can lint files at project root and in any subfolder of the project, making this plugin great for monorepos.
We never _want_ to remove things, when we're building them!
But the reality is that libraries evolve and deprecations are a fact of life.
Following are the different timeframes that we've defined as it relates to deprecating APIs in this project.
When some aspect of our API is going to be deprecated (and eventually removed), it must initially go through an RFC phase.
Whoever's motivating the removal of the api, should create an RFC issue explaining the proposal and inviting feedback from the community.
That RFC should remain active for at least 6 weeks.
The RFC text should make clear what the target date is for closing the RFC.
Once the RFC period is over, if the removal is still moving forward, the API(s) should be officially deprecated.
Once an API has been marked as deprecated, it will remain intact for at least 6 months.
After 6 months from the date of deprecation, the API is subject to removal.
See .github/CONTRIBUTING.md, then .github/DEVELOPMENT.md.
Thanks! π
Many thanks to @zetlen for creating the initial version and core infrastructure of this package! π
> π This package was templated with create-typescript-app` using the Bingo engine.