I can't add new component with angular cli in asp.net core spas

You're using the Angular CLI in a project that wasn't built with the CLI in mind, so you have to take a couple of extra steps. The error message tells you want to do.

First, run the ng g c vehicle-form command and include the --skip-import=true option.

Then, add the component declaration in the proper application module file by hand. In this case, the app.module.shared.ts, where all the other application components are declared.

Or, you can run the ng g c vehicle-form and include the --module='app.module.shared.ts'. You may have to fully qualify the path to the module file with this. I haven't tested it, so be prepared to try a few things with this on your own.

More on ng generate component: generate component.

UPDATE (2018-03-01)

Microsoft has delivered some new SPA templates for use with ASP.NET Core 2.0 applications. You can find more details about them here. The new Angular template includes support for the Angular CLI. So, if you are familiar with the CLI, then this should feel a little bit more like home. This means you can to the ng g commands to generate code with this new template.


In my case problem was in e2e project. After I move my project from angular 4 to angular 6 a new angular config file was added "angular.json" instead "angular.conf.json". In a projects section appeared two project: One of them my "ng-app" and other "ng-app-e2e". When I try execute "ng g c" command i catch a same error. A cause was in root section of "ng-app-e2e". It had empty value. I set it into "e2e". Now all is ok! This occurs despite the fact that the default project set in "ng-app".

"projects": {
    "ng-app": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      ...
    },
    "ng-app-e2e": {
      "root": "e2e", <-- SALVATION
      "sourceRoot": "e2e",
      "projectType": "application",
      ...
    }
  },
  "defaultProject": "ng-app"  

In the case of an ASP Net Core Angular project, the app.module is split across 3 files as you've highlighted. The angular CLI has to decide where to add the reference to the component in the module, so you need to provide it with some help.

ng g c moduleName --module='app.module.browser.ts'