How to create a new component in Angular 4 using CLI

surbhiGoel picture surbhiGoel · May 24, 2017 · Viewed 306.1k times · Source

In angular 2 I use

ng g c componentname

But It is not supported in Angular 4, so I created it manually, but it shows error that it is not a module.

Answer

Mike Bovenlander picture Mike Bovenlander · May 24, 2017

In Angular4 this will work the same. If you get an error I think your problem is somewhere else.

In command prompt type

ng generate component YOURCOMPONENTNAME

There are even shorthands for this: the commands generate can be used as g and component as c:

ng g c YOURCOMPONENTNAME

you can use ng --help, ng g --help or ng g c --help for the docs.

Ofcourse rename YOURCOMPONENTNAME to the name you would like to use.

Docs: angular-cli will add reference to components, directives and pipes automatically in the app.module.ts.

Update: This still functions in Angular version 8.