Skip to main content

Node.js API

npm-package-json-lint exports one object, NpmPackageJsonLint.

NpmPackageJsonLint(options)

Creates an instance of NpmPackageJsonLint

NpmPackageJsonLint has one public method, lint. lint takes a NpmPackageJsonLintOptions object.

options

Type: Object

A NpmPackageJsonLint options object.

OptionTypeDescriptionDefault
cwdstringThe current working diretory for all file operations.process.cwd()
packageJsonObjectobjectA package.json object. This must be provided or a patterns should be provided.--
packageJsonFilePathstringIf providing a package.json object, this option allows a file path to be assigned to it.--
configobjectAllows for a config object to be passed as an object via code instead of a file.--
configFilestringRelative path to a configuration file. If provided, the config in the file will be used and npm-package-json-lint will not traverse the file system to find other config files.--
patternsstring[]An array of glob patterns used to find package.json files. This must be provided or a packageJsonObject should be provided.--
quietbooleanA flag indicating whether to suppress warnings.false
ignorePathstringFile path to an ignore file.``

Example

The following example demostrates how to instantiate NpmPackageJsonLint.

const {NpmPackageJsonLint} = require('npm-package-json-lint');

const npmPackageJsonLint = new NpmPackageJsonLint({
cwd,
packageJsonObject,
packageJsonFilePath,
config,
configFile,
configBaseDirectory,
patterns,
quiet,
ignorePath
});

.lint()

Runs configured rules against the provided package.json object(s).

Example

The following example demostrates how to use lint.

const {NpmPackageJsonLint} = require('npm-package-json-lint');

const npmPackageJsonLint = new NpmPackageJsonLint({
...
});
const results = npmPackageJsonLint.lint();

Return

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,
ignoreCount: 0,
warningCount: 0
}
],
errorCount: 1,
ignoreCount: 0,
warningCount: 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.