Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Fix Angular NG3003 Import Cycle Detected

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. 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.
  2. 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 HomeComponent imports AppModule and AppModule imports HomeComponent, that's a direct cycle.
  3. 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.
  4. 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 a CommonService or a SharedModule that both components can import without creating a loop.
  5. 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.
  6. Refactor services to use the forwardRef function. 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.
  7. 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-content to project content instead of direct component references.
  8. Check for barrel files (index.ts). Sometimes the cycle is hidden because a barrel file re-exports many things. Open your index.ts files and see if they create indirect cycles. Replace barrel imports with direct file imports where possible.
  9. Rebuild the project. After making changes, run ng build or ng serve to see if the error is gone. The Angular compiler will re-evaluate the dependency graph.
  10. Verify with a clean build. If the error persists, stop the dev server, delete the dist folder, and run ng build again. 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 forwardRef for components in templates. forwardRef works 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 shared creates 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-cycle rule 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 .angular folder and run npm install again if needed.
  • Search your codebase for import ... from './index' or import ... from '..'. These barrel imports often hide cycles.
  • If you're using a monorepo, ensure that your path aliases in tsconfig.json don'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.