Angular CLI生成组件报错:Could not find an NgModule问题求助
Hey there, let's get that component generation working again! The error you're seeing happens because the Angular CLI needs to access your project's core configuration files and locate the root NgModule (usually app.module.ts) to register the new component—and you're running the command from the wrong directory.
Here's the step-by-step fix:
Navigate back to your project root directory (the
frontendfolder, notfrontend/src/app):cd ../..Or if you opened a new terminal, just navigate directly to the
frontendfolder where yourangular.jsonfile lives.Run the component generation command again:
ng generate component aboutThis time, the CLI will find your
app.module.tsfile insrc/appautomatically and add the newAboutComponentto its declarations array.
Alternative: Generate from the subdirectory (if you prefer)
If you want to stay in frontend/src/app while generating the component, you can explicitly tell the CLI where to find the NgModule using the --module flag:
ng generate component about --module ./app.module.ts
Why this happens
When you run ng generate from src/app, the CLI can't locate your project's root configuration (angular.json) or properly resolve the path to the root NgModule. Angular CLI expects to be run from the project root (where angular.json is stored) by default to access all necessary project settings.
Quick check to confirm
Make sure your frontend/src/app folder contains an app.module.ts file—this is automatically created by ng new, so it should be there. If it's missing for some reason, you'd need to recreate it, but that's unlikely given you just ran ng new.
内容的提问来源于stack exchange,提问作者Jeffrey Klinkhamer

