question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using multiple components in different modules causing "Type X is part of the declarations of 2 modules" error

See original GitHub issue

I’m submitting a … (check one with “x”)

[ ] bug report
[x] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

Right now you cannot import two directives of the same module

Expected/desired behavior

You can use same directives/components in multiple modules without errors.

What is the motivation / use case for changing the behavior?

Concept of the shared module with its shared resources is good for components that are used everywhere. But if you have a complex application (ours contain more then 50 modules and 1500 components, services and pipes all together) multiple components can be reused different way. For example QuestionListPage uses UserActiveListPanel from User module. Also PhotoListPage uses same UserActiveListPanel, and VideoListPage uses UserActiveListPanel. How do I reuse this panel? Best way is see is to provide it in directives of the component as it was before rc.5. But right now I can’t do it. But I can create lets say UserShareModule and put it there… its good, but if I put everything that is used by other modules from user module, i’ll LOT of things there unnecessary in most of times. Then what to do? Create UserActiveListPanelShareModule? It will create lot of such trash files. So what to do? Maybe we simply can provide same directives multiple times without having this annoying error Type X is part of the declarations of 2 modules?

If you tell me that its by design, then my answer is: in my opinion its a bad design.

Please tell us about your environment:

  • Angular version: 2.0.0-rc.5
  • Browser: [all ]
  • Language: [all]

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:64
  • Comments:98 (26 by maintainers)

github_iconTop GitHub Comments

199reactions
vicbcommented, Aug 12, 2016

Your question sounds like a support request.

Please use the issue tracker only for bugs and feature requests.

Use gitter and StackOverflow for support request.

142reactions
jcmordancommented, Oct 12, 2016

as @brandonroberts saids, create a shared module like this:

import { NgModule }       from '@angular/core';
import { CommonModule }   from '@angular/common';
import { FormsModule }    from '@angular/forms';

import {SharedComponentA} from "./SharedComponentA";
import {SharedComponentB} from "./SharedComponentA";

@NgModule({
    imports: [
        CommonModule,
        FormsModule,

    ],
    declarations: [
      SharedComponentA,
      SharedComponentB

    ],
    providers: [
    ],
    exports: [
      SharedComponentA,
      SharedComponentB
    ]
})
export class SharedModule {}

then use the SharedModule like this…

import { NgModule }       from '@angular/core';
import { CommonModule }   from '@angular/common';
import { FormsModule }    from '@angular/forms';

import {SharedModule } from './SharedModule';

@NgModule({
    imports: [
        CommonModule,
        FormsModule,

        SharedModule

      //..
    ],
    declarations: [
     // ....
    ],
    providers: [
        // ....
    ]
})
export class PersonModule{}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Component is part of the declaration of 2 modules
This error is considered as ng serve run time cache issue. case:1 this error will occur, once you import the component in one...
Read more >
Using multiple components in different modules ... - Reddit
I am testing the components in an angular 6 project. ... different modules causing "Type X is part of the declarations of 2...
Read more >
Angular/Ionic: Using component(s) in different modules ...
Using multiple components in different modules causing error: “ Type X is part of the declarations of 2 or more modules”. Simply… If...
Read more >
Failed: Type X is part of the declarations of 2 modules
When writing Jasmine tests, the message above is common. The failed test looks like this: failed test case. Root Cause.
Read more >
NgModule FAQ - Angular
Add only declarable classes to an NgModule's declarations list. ... A component could be imported from another application module (so you can't declare...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found