51

I have the following directory structure

enter image description here

I would like to create a new page, let's say, an About page. I want to put it in src/app/page/about/*

So I try:

ng generate component pages/about

but I get this error:

Error: More than one module matches. Use skip-import option to skip importing the component into the closest module.
More than one module matches. Use skip-import option to skip importing the component into the closest module.

Is it a good idea to use pages to store my separate pages? How can I generate components in a subdirectory using the angular-cli?

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

4 Answers4

77

Because there are 2 modules (app.module.ts, shared.module.ts) found the CLI does not know which module to declare your component in. To get around this you have to tell the CLI which module you wish to declare your component in with the module option

ng generate component pages/about --module=app.module
// or
ng generate component pages/about --module=shared.module
Brocco
  • 62,737
  • 12
  • 70
  • 76
9

To create a component as part of any new module you could also do :

cd newModule to change directory into the newModule folder

ng g c newComponent to create a component as a child of the module.

This worked out for me. I was getting same error as you received.

user3444999
  • 481
  • 4
  • 10
3

Specify Your Module

You have two modules and haven't specified which one you want to add the new component to.

If you want to add it to your app module, you can use simply:

ng generate component pages/about --module=app

And likewise to add it to your shared module, you can use:

ng generate component pages/about --module=shared

With shorthand, these commands can be shortened to:

ng g c pages/about --module=app
ng g c pages/about --module=shared

Your file structure looks off too. I would recommend moving all of the files in the pages directory up one step to the app directory.

Tony Brasunas
  • 4,012
  • 3
  • 39
  • 51
2

In general, there is need to generate component in a particular path or not more than one parent module like the above case.

$> ng g component path/component-name  
  <enter> 

No need of src/app/path/component-name ->path, if it is an Angular app. If you mention src/app, then this error occurs.

"Error: More than one module matches. Use skip-import "
Anshuman Kumar
  • 464
  • 1
  • 6
  • 20
gnganapath
  • 917
  • 12
  • 16