Fix Angular NG3003 Import Cycle Detected
In this article
This guide walks you through diagnosing and fixing the Angular NG3003 error, which occurs when two or more modules, components, or services import each other in a circular dependency. You'll learn how to identify the cycle, refactor your code to break it, and prevent it from happening again. No special tools are needed beyond your code editor and Angular CLI.
What You'll Need
- An Angular project where the NG3003 error appears
- Access to the source files (components, modules, services)
- Basic understanding of Angular modules and dependency injection
- Your preferred code editor (VS Code, WebStorm, etc.)
Step-by-Step
- Read the full error message. Angular's compiler usually tells you the exact files involved in the cycle. Look for a chain like
app.module.ts -> home.component.ts -> app.module.ts. Write down the file names and the import statements that create the loop. - Open the files listed in the error. In your editor, open each file mentioned. Look for import statements at the top that reference each other. For example, if
HomeComponentimportsAppModuleandAppModuleimportsHomeComponent, that's a direct cycle. - Identify the purpose of each import. Ask yourself: why does file A import file B? Common reasons include: using a component in a template, using a service, or accessing a constant. Knowing the reason helps you choose the right fix.
- Break the cycle by moving shared code. If two components need each other, extract the common functionality into a third file. For example, create a
shared/folder with aCommonServiceor aSharedModulethat both components can import without creating a loop. - Use lazy loading for modules. If the cycle is between feature modules, consider lazy loading one of them. In your routing configuration, use
loadChildren: () => import('./feature/feature.module').then(m => m.FeatureModule)instead of importing the module directly. This often resolves NG3003 because the lazy module is loaded on demand. - Refactor services to use the
forwardReffunction. When a service injects another service that injects back, use@Inject(forwardRef(() => OtherService))in the constructor parameter. This tells Angular to defer resolving the dependency until it's needed. - Move template references to a shared component. If a component uses another component in its template, but that other component also uses the first one, you can't have both in the same module. Instead, put one of them in a separate module and import that module, or use
ng-contentto project content instead of direct component references. - Check for barrel files (index.ts). Sometimes the cycle is hidden because a barrel file re-exports many things. Open your
index.tsfiles and see if they create indirect cycles. Replace barrel imports with direct file imports where possible. - Rebuild the project. After making changes, run
ng buildorng serveto see if the error is gone. The Angular compiler will re-evaluate the dependency graph. - Verify with a clean build. If the error persists, stop the dev server, delete the
distfolder, and runng buildagain. Sometimes stale artifacts cause false positives.
Common Mistakes
- Ignoring indirect cycles. A cycle isn't always A imports B and B imports A. It can be A imports B, B imports C, and C imports A. Always trace the full chain.
- Using
forwardReffor components in templates.forwardRefworks for dependency injection, but it doesn't fix template references. You still need to restructure the module or component. - Putting everything in the same module. If you have many components that depend on each other, a single module will likely cause cycles. Split into feature modules and import them properly.
- Deleting imports without checking usage. Removing an import that is actually used will cause a different error. Always search for usages before removing.
- Forgetting to update the module declarations. If you move a component to a new module, you must declare it there and export it if other modules need it.
Tips
- Keep your dependency graph acyclic. Think of modules as layers: core, shared, feature. Feature modules can import shared, but shared should never import feature.
- Use the Angular CLI to generate modules.
ng generate module sharedcreates a proper module file with the right structure. - Consider using standalone components. In Angular 14+, standalone components can be imported directly without a module, which often eliminates cycles.
- Use a linter. Tools like TSLint or ESLint with the
no-cyclerule can detect circular imports before compilation. - Document your module dependencies. A simple diagram or comment in the module file helps future developers avoid introducing cycles.
Troubleshooting
If the error still appears after following the steps:
- Check the exact error message again. It may point to a different cycle than the one you fixed. Repeat the process for each cycle.
- Look for cycles in your routing modules. Route definitions that import each other can cause NG3003. Use lazy loading consistently.
- Clear the Angular cache: delete the
.angularfolder and runnpm installagain if needed. - Search your codebase for
import ... from './index'orimport ... from '..'. These barrel imports often hide cycles. - If you're using a monorepo, ensure that your path aliases in
tsconfig.jsondon't point to the same file from different directions.
Frequently Asked Questions
What exactly is NG3003?
NG3003 is an Angular compiler error that indicates a circular dependency between modules, components, or services. It prevents the build because the compiler cannot resolve the order of initialization.
Can I ignore this error?
No. The build will fail, and even if you suppress it, your app may have runtime issues like undefined injections or infinite loops.
Does lazy loading always fix NG3003?
Not always, but it often does because it breaks the static import chain. If the cycle is within a single module, lazy loading won't help.
What is forwardRef and when should I use it?
forwardRef is a function that lets you reference a class before it's defined. Use it in dependency injection when two services inject each other. It's not a substitute for proper architecture.
Is NG3003 the same as a JavaScript circular import error?
They are related. NG3003 is Angular's specific error for circular imports in its dependency injection system. JavaScript may also throw its own circular import errors at runtime.
How do I prevent NG3003 in the future?
Follow a clear module hierarchy, use standalone components where possible, and run a linter that detects circular dependencies. Also, review your imports during code reviews.