Angular CLI − ng lint Command



This chapter will discuss the Angular CLI ng lint command, including its syntax, arguments, uses, options (flags), and an example demonstrating how to use this command in an Angular project.

The "ng lint" Command

The Angular CLI (Command Line Interface) provides a command called "ng lint", which is used to analyze and enforce linting rules on your Angular project codebase, helping to maintain consistent code quality and style.

In CLI, the linting rules are guidelines or standards used by the linter tool to analyze and check the source code for errors, code quality, stylistic issues, etc. In Angular or other TypeScript or JavaScript projects, the linting rules can cover the following:

  • Code Style
  • Error Prevention
  • Security
  • Performance
Note! It uses TSLint as default linting tool and uses the default configuration available in tslint.json file.

Syntax

Following is the syntax of the Angular CLI ng lint command −

ng lint ng[project] [options]

Here,

  • [project]: Specifies the name of the project you want to lint (in case you have multiple projects in your workspace).
  • [options]: Optional flags that can be used to configure the linting process.

Arguments

The argument for ng lint command is as follows −

Sr.No. Argument & Description
1 <project>

The name of the project to lint.

Options

Options are optional parameters.

Sr.No. Options & Descriptions
1 --configuration=configuration

The linting configuration to use.

Aliases: -c

2 --exclude

Files to exclude from linting.

3 --files

Files to include in linting.

4 --fix=true|false

Fixes linting errors (may overwrite linted files).

Default: false

5 --force=true|false

Succeeds even if there was linting errors.

Default: false

6 --format=format

Output format (prose, json, stylish, verbose, pmd, msbuild, checkstyle, vso, fileslist).

Default: prose

7 --help=true|false|json|JSON

Shows a help message for this command in the console.

Default: false

8 --silent=true|false

Show output text.

Default: false

9 --tsConfig=tsConfig

The name of the TypeScript configuration file.

10 --tslintConfig=tslintConfig

The name of the TSLint configuration file.

11 --typeCheck=true|false

Controls the type check for linting.

Default: false

If you run the "ng lint" command for the very first time in your project, it will display the following message:

Cannot find "lint" target for the specified project.
You can add a package that implements these capabilities.

As you can see, it suggests adding a package if you want to implement the ng lint command. So, let’s take an example and go through the steps.

Example

To work with the "ng lint" command, you need to add a package called ESLint. Here are the steps:

Step 1: Run the following command:

ng Lint

Once you run the above command for the first time in your project, it will display the following defualt message:

Cannot find "lint" target for the specified project.
You can add a package that implements these capabilities.

For example:
  ESLint: ng add angular-eslint

Would you like to add ESLint now? Yes
✔ Determining Package Manager

Step 2: To add the ESLint package, enter "y" and press enter.

Step 3: Once you press enter, it will display the following message, suggesting the installation of the ESLint package:

✔ Determining Package Manager
  › Using package manager: npm
✔ Searching for compatible package version
  › Found compatible package version: angular-eslint@19.2.1.
✔ Loading package information from registry
⠏ Confirming installation

The package angular-eslint@19.2.1 will be installed and executed.
Would you like to proceed? (Y/n)

Step 4: Again, enter "y" and press enter.

Step 5: After you press enter, it will take a while and display a confirming message as follows:

CREATE eslint.config.js (969 bytes)
UPDATE package.json (1339 bytes)
UPDATE angular.json (3098 bytes)
✔ Packages installed successfully.

Step 6: Now, run the ng lint command again.

Step 7: Once the above command is run successfully, it will display the following message:

Linting "myApp"...

All files pass linting.

The above message confirms that there are no linting errors in your code, the code quality is good, and the application is performing well.

Important! If there are any "linting errors" in your project codebase, the ng lint command will display "linting errors" or "violations" based on your linting configuration (e.g., ESLint rules).
Advertisements