When you are generating angular components either using Operating system terminal or embedded terminal in VS Code, you might have encountered this error as shown below.
Could not find an NgModule. Use the skip-import option to skip importing in NgModule.
Check the current directory at which the terminal is currently pointing or the path in which you are running this command.
Next, check if this folder contains a module or a file with .module.ts extension.
In my case I was running this command from inside the path <project name>/src while app module was located inside <project name>/src/app folder.
Navigate to the directory where the module is located and again execute this command, the error will surely go away.
Modify the command and add
--skip-importoption at the end.
This will also make the command work and the component will be generated.
When angular cli generates a component, it adds its entry in the declarations array of the module which this component will be part of.
In our case, angular cli can not find any module in which it can add the component’s entry. Thus, it throws the error.
If the command is modified with a
--skip-importoption, it works because this option is instructing angular cli not to make any entry in the module. Thus, it does not search for a module file and there is no error even if it is not present.