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



If you came here, I am guessing you are trying to use Angular CLI to generate a new component as below.

ng g component my-component

And it fails with this error ! Error: More than one module matches. Use skip-import option to skip importing the component into the closest module.

In a way the error is telling you precisely what is wrong with the project but for some reason it is not clear. Basically the problem is you have more than one Module in you Angular application at the Root Level.

So when you try generating a new component using Angular CLI, it tries to import and Register newly generated component in your app.module.ts. However you have some other module at the same root level as your AppModule and so CLI is getting confused as to which Module it should register this new Component into ?

One of the following two solutions can be used to fix this issue.

  1. Move the second Module into a Sub Folder and run the command again. This time CLI finds only one root module and registers your component into that Module.

  2. Expliclity mention which Module you want to register this component into by using following flag in your Command line. Assuming you want to register your new component under app.module.ts.

ng g my-component --module app