How to resolve “Could not find an NgModule. Use the skip-import option to skip importing in NgModule”

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.

F:\angular\src> ng g c MyComponent
Could not find an NgModule. Use the skip-import option to skip importing in NgModule.


Solution 1
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.
Solution 2
Modify the command and add --skip-import option at the end.
This will also make the command work and the component will be generated.

Reason
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-import option, 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.

If this solved your problem, please hit the clap button below or we should be looking for other possible solutions.

0

Mark Your Impression

Close Menu