What is npm-package-json-lint?
npm-package-json-lint is a tool for linting package.json files, ensuring they adhere to a defined set of rules and best practices. It helps maintain consistency and correctness in package.json files across projects.
What are npm-package-json-lint's main functionalities?
Linting package.json files
This feature allows you to define a set of rules for linting package.json files. The code sample shows a configuration file that extends the default configuration and specifies rules for the name type, version format, and requiring an author field.
module.exports = {
extends: 'npm-package-json-lint-config-default',
rules: {
'name-type': 'error',
'version-format': 'error',
'require-author': 'error'
}
};
Custom rule definitions
You can define custom rules to enforce specific requirements in your package.json files. The code sample demonstrates how to restrict certain dependencies and require the scripts field.
module.exports = {
rules: {
'no-restricted-dependencies': ['error', ['lodash', 'underscore']],
'require-scripts': 'error'
}
};
Command-line interface
The package provides a CLI for linting package.json files. The code sample shows how to run the linter using npx and a specified configuration file.
npx npm-package-json-lint . --config .npmpackagejsonlintrc.json
Other packages similar to npm-package-json-lint
eslint-plugin-json
eslint-plugin-json is an ESLint plugin that allows you to lint JSON files, including package.json. It provides a way to enforce JSON-specific rules using the familiar ESLint framework. Compared to npm-package-json-lint, it integrates JSON linting into the broader ESLint ecosystem.
jsonlint
jsonlint is a pure JavaScript library for validating and formatting JSON data. While it doesn't provide package.json-specific rules, it ensures that JSON files are syntactically correct. It is more general-purpose compared to npm-package-json-lint.
json-schema
json-schema is a library for validating JSON documents against a JSON Schema. It can be used to enforce a schema for package.json files, ensuring they adhere to a predefined structure. Unlike npm-package-json-lint, it focuses on schema validation rather than linting rules.
npm-package-json-lint
A package.json linter for Node projects
What is npm-package-json-lint?
npm-package-json-lint helps enforce standards for your package.json file.
Currently it can check for:
- validity of data types in nodes. Ex:
name
should always be a string. - whether a string is a lowercase
- whether a version number is a valid
- the presence of a given module
- the presence of a pre-release version of a module
- and much more!
Please see the wiki for a list of rules.
How do I install it?
First thing first, let's make sure you have the necessary pre-requisites.
System Dependencies
Node
Use the cli
Use cli globally
npm install npm-package-json-lint -g
Use cli in project
npm install npm-package-json-lint
Commands and configuration
Command | Alias | Description |
---|
npmPkgJsonLint --help | N/A | Lists supported CLI options |
npmPkgJsonLint --version | N/A | Lists the current version number |
npmPkgJsonLint --configFile | -c | File path to local config file or module name. |
npmPkgJsonLint --quiet | -q | Report errors only |
npmPkgJsonLint --noConfigFiles | -ncf | Skips loading project config files (i.e. .npmpackagejsonlintrc.json and npmpackagejsonlint.config.js) |
Examples
$ npmPkgJsonLint .
Looks for all package.json
files in the project. The CLI engine automatically looks for relevant config files for each package.json file that is found.
$ npmPkgJsonLint ./packages
Looks for all package.json
files in the packages
directory. The CLI engine automatically looks for relevant config files for each package.json file that is found.
$ npmPkgJsonLint ./package1 ./package2
Looks for all package.json
files in the package1
and package2
directories. The CLI engine automatically looks for relevant config files for each package.json file that is found.
$ npmPkgJsonLint -c ./config/.npmpackagejsonlintrc.json .
Looks for all package.json
files in the project. The CLI engine automatically looks for relevant config files for each package.json file that is found. The CLI also merges the config found in ./config/.npmpackagejsonlintrc.json
$ npmPkgJsonLint --configFile ./config/npmpackagejsonlint.config.json .
Same as above using the long form for specifying config files.
$ npmPkgJsonLint -q .
Looks for all package.json
files in the project. The CLI engine automatically looks for relevant config files for each package.json file that is found. Removes any warnings from the output.
$ npmPkgJsonLint --quiet ./packages
Looks for all package.json
files in the packages
directory. The CLI engine automatically looks for relevant config files for each package.json file that is found. Removes any warnings from the output using the long form for quieting output.
Lint Rules
npm-package-json-lint has a configurable set of rules. Please see the wiki for a full list of available rules. By default no rules are enabled. If you would like to use npm-package-json-lint's default ruleset, please see npm-package-json-lint-config-default.
Each rule contains the following properties:
- ID - example: require-author
- Node - example: author
- Message - example: author is required
- Rule Type - example: required
As of v2.7.0, there are multiple ways to provide a configuration object.
- Adding a
--configFile
to the command to specify a JSON file. This file is named .npmpackagejsonlintrc.json
. - Add a
npmPackageJsonLintConfig
property in package.json
file - Add a
npmpackagejsonlint.config.js
file that exports a config object in the current working directory. - Add a global
.npmpackagejsonlintrc.json
file in the root of your user directory - Add a global
npmpackagejsonlint.config.js
file that exports a config object in the root of your user directory
Configuring rules
npm-package-json-lint rules can either be run as an error
, warning
, or off
.
- "warning" - run the rule as a warning
- "error" - run the rule as an error
- "off" - disables the rule
Ex: "require-author": "error"
Migrating from v2.x.x to 3.x.x
Please see the migration guide.
Migrating from v1.x.x to 2.x.x
Please see the migration guide.
Migrating from v0.x.x to 1.x.x
Please see the migration guide.
Contributing
Please see CONTRIBUTING.md.
Release History
Please see CHANGELOG.md.
Related
License
Copyright (c) 2016-2018 Thomas Lindner. Licensed under the MIT license.