Typescript

Angular 2 component is not a known element

19 September 2026 · 11 min read

Angular 2 component is not a known element

Encountering the dreaded “Angular 2 ‘component’ is not a known element” error can be a frustrating experience for any Angular developer. This error message typically arises when Angular’s compiler cannot recognize a component selector within your templates. It suggests that either the component hasn’t been properly imported and declared in your module or there’s a mismatch in the selector name. Debugging this issue requires a systematic approach to identify the root cause and apply the correct fix. We’ll explore the common culprits behind this error and provide actionable steps to resolve it, ensuring your Angular applications run smoothly. Understanding these troubleshooting techniques will significantly improve your development workflow and prevent future occurrences of this common Angular pitfall. We’ll delve into module declarations, selector mismatches, and other potential causes, offering clear solutions and best practices along the way.

Understanding the “Angular 2 ‘component’ is not a known element” Error

The “Angular 2 ‘component’ is not a known element” error fundamentally indicates a problem with how Angular is interpreting your component structure. When Angular parses your template, it expects to find directives and components that are properly registered within the current module. If a component’s selector (the tag used to invoke it in the template) isn’t recognized, this error is thrown. This often happens because the component’s module hasn’t been imported into the module where the component is being used, or the component hasn’t been declared within that module. Think of it as trying to use a tool without first adding it to your toolbox; Angular needs explicit instructions on what components are available for use within a particular context. For example, if you create a MyCustomComponent but forget to add it to the declarations array of your AppModule, Angular won’t know what means in your template.

Several factors can lead to this error. Simple typos in the component’s selector or the module declaration are common. Another frequent cause is forgetting to import the module containing the custom component into the module where you’re trying to use it. Lazy-loaded modules can also introduce complexities, as components in those modules might not be immediately available to eagerly loaded modules. Even version mismatches between Angular packages can sometimes trigger this error, though this is less common in modern Angular projects. Essentially, the error boils down to Angular being unable to find the component definition when it encounters its selector in a template. A systematic approach is crucial when debugging, ensuring each potential cause is investigated.

To further clarify, consider this analogy: Imagine you’re building a house. You have various tools like hammers, saws, and drills. If you try to use a tool that isn’t in your toolbox (i.e., not declared in your Angular module), you’ll run into problems. Similarly, if you misspell the name of the tool (i.e., the component selector), you won’t be able to use it correctly. Keeping your “toolbox” (module) well-organized and ensuring you have the right tools (components) properly declared is vital for a successful Angular project. According to the official Angular documentation, “Every component must be declared in exactly one NgModule” [Angular Documentation].

Troubleshooting Steps: Identifying the Root Cause

When faced with the “Angular 2 ‘component’ is not a known element” error, a methodical approach to troubleshooting is essential. Start by verifying the component’s selector. Ensure that the selector used in your template exactly matches the selector defined in the component’s @Component decorator. Case sensitivity matters, so double-check for any capitalization discrepancies. Next, confirm that the component is declared in the appropriate module. Open the module where you’re trying to use the component and inspect the declarations array. If the component is missing, add it. This is perhaps the most common cause of the error.

After checking the selector and module declaration, verify that the module containing the component is imported into the module where you’re using it. If the component resides in a separate module, you must import that module into the module that needs to use the component. This is done by adding the module to the imports array of the parent module. This step is often overlooked, especially in larger projects with many modules. If your application uses lazy-loaded modules, ensure that the component is being accessed correctly within the lazy-loaded context. Finally, clear your browser’s cache and restart your Angular development server. Sometimes, outdated cached files can interfere with Angular’s compilation process. This step can often resolve seemingly inexplicable errors.

Here’s a featured snippet-optimized paragraph summarizing the key troubleshooting steps: To resolve the “Angular 2 ‘component’ is not a known element” error, first, meticulously verify the component’s selector for any typos or case-sensitivity issues. Second, confirm that the component is declared in the declarations array of the relevant module. Third, ensure that the module containing the component is imported into the module where the component is being used. Clearing the browser cache and restarting the development server can also sometimes resolve the issue, especially after making changes to module declarations. These steps will help you systematically identify and fix the underlying cause of the error.

Common Solutions and Code Examples

Let’s explore some practical solutions to address the “Angular 2 ‘component’ is not a known element” error with concrete code examples. The most frequent fix involves ensuring the component is properly declared within its module. Open the component’s module file (e.g., my-component.module.ts) and verify that your component is listed in the declarations array. If it’s missing, add it like this:

import { NgModule } from '@angular/core'; import { MyCustomComponent } from './my-custom.component'; @NgModule({ declarations: [ MyCustomComponent // Add the component here ], imports: [], exports: [ MyCustomComponent //Optional: if you want to use the component in other modules ] }) export class MyCustomModule { } 

Next, ensure that this module (MyCustomModule in this example) is imported into the module where you’re trying to use the component. This is typically your AppModule or another feature module. Add the MyCustomModule to the imports array of the parent module:

import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { MyCustomModule } from './my-component/my-component.module'; // Import the module import { AppComponent } from './app.component'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, MyCustomModule // Import the module here ], providers: [], bootstrap: [AppComponent] }) export class AppModule { } 

If you’re using components from Angular Material or other third-party libraries, make sure you’ve imported the corresponding modules from those libraries. For instance, if you’re using MatButton, you need to import MatButtonModule from @angular/material/button. The Angular Material website provides detailed instructions on how to install and import each component module [Angular Material]. These steps will ensure that Angular can correctly resolve the component’s selector and avoid the “not a known element” error.

Best Practices to Prevent Future Errors

Preventing the “Angular 2 ‘component’ is not a known element” error requires adherence to best practices in Angular development. One key practice is to organize your components into logical modules. Group related components together within a feature module, making it easier to manage dependencies and ensure proper declarations. This modular approach promotes code reusability and maintainability. Furthermore, always double-check your component selectors and module declarations after creating a new component. A simple typo can easily lead to this error.

Another helpful strategy is to use a consistent naming convention for your components, modules, and selectors. This reduces the likelihood of errors caused by misremembering names or inconsistent capitalization. Consider using a prefix for your component selectors to avoid naming conflicts with existing HTML elements or third-party components. For example, instead of using , use . Finally, leverage your IDE’s code completion and linting features to catch potential errors early. Most IDEs provide Angular-specific support that can automatically detect missing declarations and incorrect selectors. These tools can significantly improve your development efficiency and reduce the chances of encountering this error.

Here are some additional best practices to keep in mind:

  • Always declare components in their respective modules.
  • Import necessary modules into parent modules.
  • Use consistent naming conventions.
Infographic here
FAQ: Frequently Asked Questions -------------------------------
Why am I still getting the error even after declaring and importing the component?
Double-check for typos in the selector within your template. Ensure it matches the selector defined in the component's @Component decorator exactly. Also, verify that the module containing the component is imported only once to avoid circular dependencies.
Does the order of imports in the module matter?
In most cases, the order of imports doesn't matter. However, if you have modules with circular dependencies, the order might become relevant. It's best to avoid circular dependencies altogether to prevent unexpected behavior.
How do I fix this error in a lazy-loaded module?
Ensure that the component is declared within the lazy-loaded module and that the module is properly configured for lazy loading. Also, verify that you're not trying to access the component from an eagerly loaded module without importing the lazy-loaded module.
1. Verify the component selector for typos. 2. Check the module declaration for the component. 3. Ensure the module is imported into the parent module. 4. Clear browser cache and restart the server. 5. Consult the Angular documentation \[[Angular Docs](https://angular.io/docs)\].
  • Double check the component’s selector.
  • Make sure the component is declared in the module.

By carefully reviewing these steps, you’ll be well-equipped to diagnose and resolve the “Angular 2 ‘component’ is not a known element” error. This issue, while initially perplexing, becomes much simpler to tackle with a systematic approach and a solid understanding of Angular’s module system.

Debugging errors like “Angular 2 ‘component’ is not a known element” is a crucial part of becoming a proficient Angular developer. Mastering these techniques will not only save you time but also deepen your understanding of Angular’s architecture. So, next time you encounter this error, remember to systematically check your selectors, module declarations, and imports. By applying these principles, you’ll be able to quickly resolve the issue and get back to building amazing Angular applications. Don’t forget to explore other common Angular errors and troubleshooting tips to further enhance your skills. Happy coding!

Question & Answer :
I’m trying to use a component I created inside the AppModule in other modules. I get the following error though:

“Uncaught (in promise): Error: Template parse errors:

‘contacts-box’ is not a known element:

  1. If ‘contacts-box’ is an Angular component, then verify that it is part of this module.
  2. If ‘contacts-box’ is a Web Component then add ‘CUSTOM_ELEMENTS_SCHEMA’ to the ‘@NgModule.schemas’ of this component to suppress this message.

My project structure is quite simple: Overall project structure

I keep my pages in pages directory, where each page is kept in different module (e.g. customers-module) and each module has multiple components (like customers-list-component, customers-add-component and so on). I want to use my ContactBoxComponent inside those components (so inside customers-add-component for example).

As you can see I created the contacts-box component inside the widgets directory so it’s basically inside the AppModule. I added the ContactBoxComponent import to app.module.ts and put it in declarations list of AppModule. It didin’t work so I googled my problem and added ContactBoxComponent to export list as well. Didn’t help. I also tried putting ContactBoxComponent in CustomersAddComponent and then in another one (from different module) but I got an error saying there are multiple declarations.

What am I missing?

These are the 5 steps I perform when I get such an error.

  • Are you sure the name is correct? (Also check the selector defined in the component)
  • Declare the component in a module?
  • If it is in another module, export the component?
  • If it is in another module, import that module?
  • Restart the cli?

When the error occurs during unit testing, make sure your declared the component or imported the module in TestBed.configureTestingModule

I also tried putting ContactBoxComponent in CustomersAddComponent and then in another one (from different module) but I got an error saying there are multiple declarations.

You can’t declare a component twice. You should declare and export your component in a new separate module. Next, you should import this new module in every module you want to use your component.

It is hard to tell when you should create a new module and when you shouldn’t. I usually create a new module for every component I reuse. When I have some components that I use almost everywhere I put them in a single module. When I have a component that I don’t reuse I won’t create a separate module until I need it somewhere else.

Though it might be tempting to put all your components in a single module, this is bad for the performance. While developing, a module has to recompile every time changes are made. The bigger the module (more components) the more time it takes. Making a small change to a big module takes more time than making a small change in a small module.

EDIT for v15

Angular 15 introduced so-called ‘Standalone’ components. These components DO NOT need to be declared in a module.

@Component({ selector: 'app-hello', standalone: true, <------ NOTE THIS template: 'hello world', }) export class StandaloneComponent {} 

Using a standalone component in a module component:

@NgModule({ imports: [ CommonModule, StandaloneComponent ], declarations: [ModuleComponent], exports: [ModuleComponent], }) export class ExampleModule {} 

Using a module component in a standalone component:

@Component({ selector: 'app-hello', standalone: true, template: 'hello world <module-component></module-component>', imports: [ExampleModule] }) export class StandaloneComponent {}