A package.json linter for Node projects
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.
First thing first, let's make sure you have the necessary pre-requisites.
npm install npm-package-json-lint -g
npm install npm-package-json-lint
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) |
$ 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 thepackages
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 thepackage1
andpackage2
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 thepackages
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.
npm-package-json-lint exports two main objects: CLIEngine
and NpmPackageJsonLint
.
Creates an instance of NpmPackageJsonLint
NpmPackageJsonLint
has one public method, lint
. lint
takes a package.json object in object form and a config object as parameters.
Runs configured rules against the provided package.json object.
Type: object
A package.json file in object form.
Type: object
A valid configuration object.
The following example demostrates how to use lint
.
const NpmPackageJsonLint = require('npm-package-json-lint').NpmPackageJsonLint;
const npmPackageJsonLint = new NpmPackageJsonLint();
const results = npmPackageJsonLint.lint(packageJsonDataAsObject, configObject);
lint
returns an object with an array of LintIssue
s. Please see LintIssue
section for more detail.
{
issues: [
{
lintId: 'require-name',
severity: 'error',
node: 'name',
lintMessage: 'name is required'
}
]
}
Calling .version
on an instance of NpmPackageJsonLint
will return the version number of npm-package-json-lint that the linter is associated with.
const NpmPackageJsonLint = require('npm-package-json-lint').NpmPackageJsonLint;
const npmPackageJsonLint = new NpmPackageJsonLint();
npmPackageJsonLint.version; // => '3.0.0'
Creates an instance of CLIEngine
Type: object
CLIEngine configuration object
configFile
{string} Name of module/file to use.cwd
{string} The current working diretory for all file operations.useConfigFiles
{boolean} False disables use of .npmpackagejsonlintrc.json files and npmpackagejsonlint.config.js files.rules
{object} An object of rules to use.
The following example demostrates how to initialize a CLIEngine
.
const CLIEngine = require('npm-package-json-lint').CLIEngine;
const cliEngineOptions = {
configFile: '',
cwd: process.cwd(),
useConfigFiles: true,
rules: {}
};
const cliEngine = new CLIEngine(cliEngineOptions);
Runs npm-package-json-lint against the array a patterns.
Type: array
An array of glob patterns
The following example demostrates how to use executeOnPackageJsonFiles
.
const CLIEngine = require('npm-package-json-lint').CLIEngine;
const cliEngineOptions = {
configFile: '',
cwd: process.cwd(),
useConfigFiles: true,
rules: {}
};
const patterns = ['.'];
const cliEngine = new CLIEngine(cliEngineOptions);
const results = cliEngine.executeOnPackageJsonFiles(patterns);
executeOnPackageJsonFiles
returns an object with an array of results.
{
results: [
{
filePath: './package.json',
issues: [
{
lintId: 'require-name',
severity: 'error',
node: 'name',
lintMessage: 'name is required'
}
],
errorCount: 1,
warningCount: 0
}
],
errorCount: 1,
warningCount: 0
}
Calling .version
on an instance of CLIEngine
will return the version number of npm-package-json-lint that the CLIEngine is associated with.
const CLIEngine = require('npm-package-json-lint').CLIEngine;
const cliEngineOptions = {
configFile: '',
cwd: process.cwd(),
useConfigFiles: true,
rules: {}
};
const cliEngine = new CLIEngine(cliEngineOptions);
cliEngine.version; // => '3.0.0'
WARNING
Only the functions documented above are supported. All other functions that are exposed may change with any release. Please refrain from using them.
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 inpackage.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
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"
Please see the migration guide.
Please see the migration guide.
Please see the migration guide.
Please see CONTRIBUTING.md.
Please see CHANGELOG.md.
- grunt-npm-package-json-lint: Grunt Wrapper for npm-package-json-lint
- npm-package-json-lint-config-default: Shared default configuration module for npm-package-json-lint
Copyright (c) 2016-2019 Thomas Lindner. Licensed under the MIT license.