Angular CLI - ng generate Command



The ng generate is a built-in command of Angular CLI. It is used to generate and/or modifies files related to various Angular entities like components, directives, services, modules, pipes, interfaces and more.

If you want, you can manually create those classes and files without using the ng generate command in three simple steps. Create the required files yourself, write the code for each file, and update the relevant module or other configurations. However, it will consume more time.

Syntax

The syntax of ng generate command is as follows −

ng generate  [options]
Or,
ng g  [options]

Argument

The argument for ng generate command is as follows −

Sr.No. Argument & Syntax Description
1 The schematic or collection:schematic to generate. This option can take one of the following sub-commands:
  • appShell

  • application

  • class

  • component

  • directive

  • enum

  • guard

  • interceptor

  • interface

  • library

  • module

  • pipe

  • service

  • serviceWorker

  • webWorker

Options

The table below contains all the optional parameters of ng generate command −

Sr.No. Option & Syntax Description
1 --defaults=true|false By default its value is "false". When this option is "true", it disables interactive input prompts for options with a default.
2 --dryRun=true|false

When "true", runs through and shows action to be taken without applying changes to the files or the project.

Default: false.

Aliases: -d.

3 --force=true|false

When "true", forces overwriting of existing files.

Default: false.

Aliases: -f.

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

If you need any help with this command, use --help along with the command in console.

Default: false.

5 --interactive=true|false When "false", disables interactive input prompts. By default, it is "true".

Example 1

Let's see how we can create a component in Angular application using the ng generate command −

> ng generate component goals
CREATE src/app/goals/goals.component.html (20 bytes)
CREATE src/app/goals/goals.component.spec.ts (621 bytes)
CREATE src/app/goals/goals.component.ts (271 bytes)
CREATE src/app/goals/goals.component.css (0 bytes)

Example 2

In this example, we are going to create a Service using ng generate command:

> ng generate service print-name
CREATE src/app/print-name.service.spec.ts (389 bytes)
CREATE src/app/print-name.service.ts (147 bytes)

Similarly, you can use ng generate command to generate other Angular entities.

Advertisements