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.
This helps to ensure that your files are of the highest standard and quality.
It is also customisable to match your specific project guidline requirements/desires.
It has support for both command line and programmatic usage.
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
- the presence of an author
- the presence of any bugs
- valid dependencies
- valid licenses
- and much more!
Please see the website for a list of rules.
Install and Use
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
Using the linter
From the command line
- Once npm-package-json-lint is installed either in the project or globally it can be run with the
following command,
npx npm-package-json-lint
. This will go through the process of linting/validating
your code to meet your desired specifications.
In the program
-
The linter can also be used as a module in the program and the output be displayed via a log message.
Adding the following code to your file can accomplish this.
const npmPackageJsonLint = require("npm-package-json-lint");
const result = npmPackageJsonLint.lintFiles(["package.json"]);
console.log(result);
Custom Configuration
npm-package-json-lint has the ability to be constomly configured to match your desires.
To configure the linter navigate to the .npmpackagejsonlintrc file in the repository and add
in or remove your desired rules.
Local Development
Follow these steps to get your local environement set up to allow you to contribute to the repository
- In the terminal, navigate to the directory in which you want the repository to be cloned.
- Run this command from the terminal,
git clone https://github.com/tclindner/npm-package-json-lint.git
- After cloning the repository navigate into the project's root directory from the terminal.
- Run "npm install" to install the necessary dependencies.
- Your local environment is now set up to create PR's and work on different issues
Documentation
Website
Quick links
CLI commands and configuration | Node.js API | Integrations
Migrating from v6.x.x to 7.x.x
Please see the migration guide.
Migrating from v5.x.x to 6.x.x
Please see the migration guide.
Migrating from v4.x.x to 5.x.x
Please see the migration guide.
Migrating from v3.x.x to 4.x.x
Please see the migration guide.
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-2023 Thomas Lindner. Licensed under the MIT license.