ng generate

From Get docs
< CLI Overview and Command ReferenceAngular/docs/10/cli/generate


ng generate

Generates and/or modifies files based on a schematic.

ng generate <schematic> [options]

ng g <schematic> [options]

Arguments

Argument Description
<schematic>

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

Option Description
false When true, disables interactive input prompts for options with a default.
false

When true, runs through and reports activity without writing out results.

Default: false

Aliases: -d

false

When true, forces overwriting of existing files.

Default: false

Aliases: -f

false|json|JSON

Shows a help message for this command in the console.

Default: false

false When false, disables interactive input prompts.

Schematic commands

appShell

ng generate appShell [options]

ng g appShell [options]

Generates an app shell for running a server-side version of an app.

Options

Option Description
--appDir=appDir

The name of the application directory.

Default: app

--appId=appId

The app ID to use in withServerTransition().

Default: serverApp

--clientProject=clientProject The name of the related client app.
--main=main

The name of the main entry-point file.

Default: main.server.ts

--rootModuleClassName= rootModuleClassName

The name of the root module class.

Default: AppServerModule

--rootModuleFileName= rootModuleFileName

The name of the root module file

Default: app.server.module.ts

--route=route

Route path used to produce the app shell.

Default: shell

--tsconfigFileName= tsconfigFileName

Deprecated: This option has no effect.

The name of the TypeScript configuration file.

Default: tsconfig.server

application

ng generate application <name> [options]

ng g application <name> [options]

Generates a new basic app definition in the "projects" subfolder of the workspace.

Arguments

Argument Description
<name> The name of the new app.

Options

Option Description
false

When true, includes styles inline in the root component.ts file. Only CSS styles can be included inline. Default is false, meaning that an external styles file is created and referenced in the root component.ts file.

Default: false

Aliases: -s

false

When true, includes template inline in the root component.ts file. Default is false, meaning that an external template file is created and referenced in the root component.ts file.

Default: false

Aliases: -t

false

Add support for legacy browsers like Internet Explorer using differential loading.

Default: false

false

When true, applies lint fixes after generating the application.

Default: false

false

When true, creates a bare-bones project without any testing frameworks. (Use for learning purposes only.)

Default: false

--prefix=prefix

A prefix to apply to generated selectors.

Default: app

Aliases: -p

false

When true, creates a routing NgModule.

Default: false

false

Skip installing dependency packages.

Default: false

false

When true, does not add dependencies to the "package.json" file.

Default: false

false

When true, does not create "spec.ts" test files for the app.

Default: false

Aliases: -S

false

Creates an application with stricter build optimization options.

Default: false

scss|sass|less|styl

The file extension or preprocessor to use for style files.

Default: css

Native|None|ShadowDom The view encapsulation strategy to use in the new app.

class

ng generate class <name> [options]

ng g class <name> [options]

Creates a new generic class definition in the given or default project.

Arguments

Argument Description
<name> The name of the new class.

Options

Option Description
false

When true, applies lint fixes after generating the class.

Default: false

--project=project The name of the project.
false

When true, does not create "spec.ts" test files for the new class.

Default: false

--type=type

Adds a developer-defined type to the filename, in the format "name.type.ts".

Default:

component

ng generate component <name> [options]

ng g component <name> [options]

Creates a new generic component definition in the given or default project.

Arguments

Argument Description
<name> The name of the component.

Options

Option Description
OnPush

The change detection strategy to use in the new component.

Default: Default

Aliases: -c

false

Specifies if the style will contain :host { display: block; }.

Default: false

Aliases: -b

false

Deprecated: Since version 9.0.0 with Ivy, entryComponents is no longer necessary.

When true, the new component is the entry component of the declaring NgModule.

Default: false

false

When true, the declaring NgModule exports this component.

Default: false

false

When true, creates the new files at the top level of the current project.

Default: false

false

When true, includes styles inline in the component.ts file. Only CSS styles can be included inline. By default, an external styles file is created and referenced in the component.ts file.

Default: false

Aliases: -s

false

When true, includes template inline in the component.ts file. By default, an external template file is created and referenced in the component.ts file.

Default: false

Aliases: -t

false

When true, applies lint fixes after generating the component.

Default: false

--module=module

The declaring NgModule.

Aliases: -m

--prefix=prefix

The prefix to apply to the generated component selector.

Aliases: -p

--project=project The name of the project.
--selector=selector The HTML selector to use for this component.
false

When true, does not import this component into the owning NgModule.

Default: false

false

Specifies if the component should have a selector or not.

Default: false

false

When true, does not create "spec.ts" test files for the new component.

Default: false

scss|sass|less|styl

The file extension or preprocessor to use for style files.

Default: css

--type=type

Adds a developer-defined type to the filename, in the format "name.type.ts".

Default: Component

Native|None|ShadowDom

The view encapsulation strategy to use in the new component.

Aliases: -v

directive

ng generate directive <name> [options]

ng g directive <name> [options]

Creates a new generic directive definition in the given or default project.

Arguments

Argument Description
<name> The name of the new directive.

Options

Option Description
false

When true, the declaring NgModule exports this directive.

Default: false

false

When true (the default), creates the new files at the top level of the current project.

Default: true

false

When true, applies lint fixes after generating the directive.

Default: false

--module=module

The declaring NgModule.

Aliases: -m

--prefix=prefix

A prefix to apply to generated selectors.

Aliases: -p

--project=project The name of the project.
--selector=selector The HTML selector to use for this directive.
false

When true, does not import this directive into the owning NgModule.

Default: false

false

When true, does not create "spec.ts" test files for the new class.

Default: false

enum

ng generate enum <name> [options]

ng g enum <name> [options]

Generates a new, generic enum definition for the given or default project.

Arguments

Argument Description
<name> The name of the enum.

Options

Option Description
false

When true, applies lint fixes after generating the enum.

Default: false

--project=project The name of the project in which to create the enum. Default is the configured default project for the workspace.

guard

ng generate guard <name> [options]

ng g guard <name> [options]

Generates a new, generic route guard definition in the given or default project.

Arguments

Argument Description
<name> The name of the new route guard.

Options

Option Description
false

When true (the default), creates the new files at the top level of the current project.

Default: true

--implements Specifies which interfaces to implement.
false

When true, applies lint fixes after generating the guard.

Default: false

--project=project The name of the project.
false

When true, does not create "spec.ts" test files for the new guard.

Default: false

interceptor

ng generate interceptor <name> [options]

ng g interceptor <name> [options]

Creates a new, generic interceptor definition in the given or default project.

Arguments

Argument Description
<name> The name of the interceptor.

Options

Option Description
false

When true (the default), creates files at the top level of the project.

Default: true

false

When true, applies lint fixes after generating the interceptor.

Default: false

--project=project The name of the project.
false

When true, does not create "spec.ts" test files for the new interceptor.

Default: false

interface

ng generate interface <name> <type> [options]

ng g interface <name> <type> [options]

Creates a new generic interface definition in the given or default project.

Arguments

Argument Description
<name> The name of the interface.
<type> Adds a developer-defined type to the filename, in the format "name.type.ts".

Options

Option Description
false

When true, applies lint fixes after generating the interface.

Default: false

--prefix=prefix

A prefix to apply to generated selectors.

Default:

--project=project The name of the project.

library

ng generate library <name> [options]

ng g library <name> [options]

Creates a new generic library project in the current workspace.

Arguments

Argument Description
<name> The name of the library.

Options

Option Description
--entryFile=entryFile

The path at which to create the library's public API file, relative to the workspace root.

Default: public-api

false

When true, applies lint fixes after generating the library.

Default: false

--prefix=prefix

A prefix to apply to generated selectors.

Default: lib

Aliases: -p

false

When true, does not install dependency packages.

Default: false

false

When true, does not add dependencies to the "package.json" file.

Default: false

false

When true, does not update "tsconfig.json" to add a path mapping for the new library. The path mapping is needed to use the library in an app, but can be disabled here to simplify development.

Default: false

module

ng generate module <name> [options]

ng g module <name> [options]

Creates a new generic NgModule definition in the given or default project.

Arguments

Argument Description
<name> The name of the NgModule.

Options

Option Description
false

When true, creates the new files at the top level of the current project root.

Default: false

false

When true, applies lint fixes after generating the module.

Default: false

--module=module

The declaring NgModule.

Aliases: -m

--project=project The name of the project.
--route=route The route path for a lazy-loaded module. When supplied, creates a component in the new module, and adds the route to that component in the Routes array declared in the module provided in the --module option.
false

When true, creates a routing module.

Default: false

Root

The scope for the new routing module.

Default: Child

pipe

ng generate pipe <name> [options]

ng g pipe <name> [options]

Creates a new generic pipe definition in the given or default project.

Arguments

Argument Description
<name> The name of the pipe.

Options

Option Description
false

When true, the declaring NgModule exports this pipe.

Default: false

false

When true (the default) creates files at the top level of the project.

Default: true

false

When true, applies lint fixes after generating the pipe.

Default: false

--module=module

The declaring NgModule.

Aliases: -m

--project=project The name of the project.
false

When true, does not import this pipe into the owning NgModule.

Default: false

false

When true, does not create "spec.ts" test files for the new pipe.

Default: false

service

ng generate service <name> [options]

ng g service <name> [options]

Creates a new, generic service definition in the given or default project.

Arguments

Argument Description
<name> The name of the service.

Options

Option Description
false

When true (the default), creates files at the top level of the project.

Default: true

false

When true, applies lint fixes after generating the service.

Default: false

--project=project The name of the project.
false

When true, does not create "spec.ts" test files for the new service.

Default: false

serviceWorker

ng generate serviceWorker [options]

ng g serviceWorker [options]

Pass this schematic to the "run" command to create a service worker

Options

Option Description
--configuration=configuration

The configuration to apply service worker to.

Default: production

--project=project The name of the project.
--target=target

The target to apply service worker to.

Default: build

webWorker

ng generate webWorker <name> [options]

ng g webWorker <name> [options]

Creates a new generic web worker definition in the given or default project.

Arguments

Argument Description
<name> The name of the worker.

Options

Option Description
--project=project The name of the project.
false

Add a worker creation snippet in a sibling file of the same name.

Default: true

--target=target

The target to apply web worker to.

Default: build


© 2010–2020 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://v10.angular.io/cli/generate