Next.js - CLI lint Command



In Next.js CLI, the `lint` command is used to run ESLint on your application code. This command helps you catch potential issues in Next.js code by analyzing all the directories or selected specific directories. In this chapter, we will explain how to use the `lint` command and its available options to simplify the linting process.

Next.js Lint Command Syntax

Following is the syntax of the lint command in Next.js CLI.

npx next lint [options]

For example, npx next lint --ext .ts,.tsx, specifies the file extensions to lint as `.ts` and `.tsx`.

Options of Lint Command

Below is a list of options available for the `lint` command.

Options Explanation
[directory] Specifies a base directory to lint the application. If not provided, the current directory is used.
-d, --dir Include directory or directories to run ESLint.
--file Include file or files to run ESLint.
--ext Specify JavaScript file extensions to lint.
-c, --config Uses a configuration file, overriding all other configuration options.
--strict Creates a .eslintrc.json file using the Next.js strict configuration.
--rulesdir Uses additional rules from this directory or directories.
--fix Automatically fix linting issues.
--fix-type Specify the types of fixes to apply (e.g., problem, suggestion, layout).
--ignore-path Specify a file to ignore.
--no-ignore Disables the --ignore-path option.
--quiet Reports errors only, suppressing warnings.
--max-warnings Specify the number of warnings before triggering a non-zero exit code. Default: -1.
-o, --output-file Specify a file to write the linting report to.
-f, --format Uses a specific output format for linting results.
--no-inline-config Prevents comments from changing the config or rules.
--report-unused-disable-directives-severity Specify severity level for unused eslint-disable directives. Choices: "error", "off", "warn".
--no-cache Disables caching of linting results.
--cache-location Specify a location for cache.
--cache-strategy Specify a strategy to use for detecting changed files in the cache. Default: "metadata".
--error-on-unmatched-pattern Reports errors when any file patterns are unmatched.
-h, --help Displays the help message for the lint command.

Lint Command with Ignore Path

In Next.js, we can use the `--ignore-path` option to specify a file that should be ignored during linting.

npx next lint --ignore-path .eslintignore

After running the above command in your terminal, the files specified in the `.eslintignore` file will be ignored during linting.

Output

In the output, you can see that warnings and errors are reported.

next.js-lint-command

Lint Command with Quiet Option

In Next.js, we can use the `--quiet` option to hide warnings and report only errors.

npx next lint --quiet

Output

In the output, you can see that no errors are found, and all warnings are ignored.

next.js-lint-command-quiet
Advertisements