The Linter class is a low-level API for verifying JavaScript code. Unlike the ESLint class, Linter does not handle file I/O or configuration file loading. It works directly with code strings and configuration objects.
Constructor
const { Linter } = require ( "eslint" );
const linter = new Linter ( options );
Configuration options for the Linter instance. Path to a directory that should be considered as the current working directory. Defaults to process.cwd().
Array of feature flags to enable.
The type of config used. Must be "flat". Retained for backwards compatibility.
Static Properties
Linter.version
const version = Linter . version ;
The ESLint version string.
Instance Methods
verify()
Verifies text against the specified rules.
const messages = linter . verify ( textOrSourceCode , config , filenameOrOptions );
textOrSourceCode
string | SourceCode
required
The JavaScript code to verify, or a SourceCode object.
config
Config | Config[]
required
The configuration object or array of configuration objects.
The filename or options object. The filename of the source code.
Allow/disallow inline comments’ ability to change config.
If true, the linter doesn’t make fix properties in the result.
reportUnusedDisableDirectives
boolean | 'off' | 'warn' | 'error'
Adds reported errors for unused eslint-disable directives.
A predicate function that determines whether a given rule should run.
Array of lint messages. Show LintMessage properties
The rule ID that generated this message.
The severity of the message: 1 for warning, 2 for error.
The 1-based line number where the issue occurs.
The 1-based column number where the issue occurs.
The 1-based line number where the issue ends.
The 1-based column number where the issue ends.
The fix object if the rule provides a fix.
Array of suggestion objects if the rule provides suggestions.
verifyAndFix()
Performs multiple autofix passes over the text.
const result = linter . verifyAndFix ( text , config , filenameOrOptions );
The source text to apply fixes to.
config
Config | Config[]
required
The ESLint config object or array.
The filename or options object. The filename of the source code.
fix
boolean | function
default: "true"
Whether fixes should be applied. Can be a predicate function.
Allow/disallow inline config.
Whether any fixes were applied.
Array of remaining lint messages.
getSourceCode()
Gets the SourceCode object from the last verification.
const sourceCode = linter . getSourceCode ();
The SourceCode object from the last verify() call, or null.
getSuppressedMessages()
Gets suppressed messages from the last verification.
const suppressedMessages = linter . getSuppressedMessages ();
Array of messages that were suppressed by directives.
getTimes()
Gets timing information from the last verification (when stats are enabled).
const times = linter . getTimes ();
Array of timing information for each pass.
getFixPassCount()
Gets the number of autofix passes made in the last run.
const count = linter . getFixPassCount ();
The number of autofix passes.
hasFlag()
Checks if a feature flag is enabled.
const enabled = linter . hasFlag ( flag );
The feature flag name to check.
true if the flag is enabled, false otherwise.
Examples
Basic Verification
Auto-fixing
With Filename
Custom CWD
const { Linter } = require ( "eslint" );
const linter = new Linter ();
const code = "var foo = 'bar';" ;
const config = {
rules: {
"no-var" : "error" ,
"quotes" : [ "error" , "double" ]
}
};
const messages = linter . verify ( code , config );
console . log ( messages );
// [
// {
// ruleId: 'no-var',
// severity: 2,
// message: 'Unexpected var, use let or const instead.',
// line: 1,
// column: 1,
// ...
// },
// ...
// ]
const { Linter } = require ( "eslint" );
const linter = new Linter ();
const code = "var foo = 'bar';" ;
const config = {
rules: {
"quotes" : [ "error" , "double" ]
}
};
const result = linter . verifyAndFix ( code , config );
console . log ( result );
// {
// fixed: true,
// output: 'var foo = "bar";',
// messages: []
// }
const { Linter } = require ( "eslint" );
const linter = new Linter ();
const code = "console.log('test');" ;
const config = {
rules: {
"no-console" : "error"
}
};
const messages = linter . verify ( code , config , {
filename: "test.js" ,
allowInlineConfig: false
});
console . log ( messages );
const { Linter } = require ( "eslint" );
const path = require ( "path" );
const linter = new Linter ({
cwd: path . join ( __dirname , "src" )
});
const code = "var x = 1;" ;
const config = {
rules: { "no-var" : "error" }
};
const messages = linter . verify ( code , config );
console . log ( messages );
Differences from ESLint Class
The Linter class differs from the ESLint class in several ways:
Feature Linter ESLint File I/O No Yes Config file loading No Yes Caching No Yes Formatters No Yes Glob patterns No Yes Synchronous API Yes No Use case Low-level, in-memory linting Full linting workflow
Use Linter when you need fine-grained control and already have the code and configuration in memory. Use ESLint for a complete linting workflow with file handling.