-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error "Please add a @NgModule annotation" in Angular4 #15763
Comments
Please add a small reproduction. |
I can´t create a plunkr with this setup nor upload this to my git account as it's not open source code. I could try to create another 2 simple repos to reproduce this same scenario, but I'd need to confirm first that I can import a repo generated with Angular 2 from another project in Angular 4. That should not be an issue, right? |
yes, it should work without any issue. is |
It's my own module. It contains some common components used by different projects. |
What are your dependencies on your You have to set version in your "library" to If it is there, that is the problem. I hope it helps, if not i can explain better :). |
Thanks @kukjevov so it really is a problem if my library uses Angular 2 and my app uses Angular 4. If I use @Toxicable thanks for your input, I think that's exactly what's happening. |
Any component written for 2.x should work for 4.x. It's important that you do not generate the factories in your library code. Your library code should only emit the metadata.json files. Each application should then import NgModules from the shared library code, and compile each application as a unit. This is baked into the entire design of angular's compiler - backwards compatibility. This works because the metadata.json is backwards compatible, and ngFactory code can be generated for each version when an app is compiled. |
Thanks @robwormald could you please elaborate or give me a hint on where to look for some more info about it? My common module inside node_modules has a my-common-module.d.ts with the exports, including |
hi @davidanaya well if you use I am not completely sure, but i think this should work as far as your code will be backward compatible. It does not matter if your library is compiled with 2.4.3, or 4.0.1 if code compiles and works with both of them. |
Thanks @kukjevov I'll give it a try. |
Well, that worked! So, just to summarize in case someone else needs it. In my common library I used this in my package.json
I did this in a different branch,
Looks promising; if I'm not wrong it took the last available version for @angular/common, which is 4.0.1 Now I push the branch to my github repo and change to my app project. I run
I run it, and it works 👍 |
Hello, compiler.es5.js:14577 Uncaught Error: Unexpected value 'LoadingComponent' declared by the module 'AppModule'. Please add a @Pipe/@Directive/@component annotation. But LoadingComponent is correct: import {Component, Input} from '@angular/core'; When I remove this component it complains on another and so on. My AppModule: @NgModule({ export class AppModule { Any ideas why it might happen and how to solve it? |
Just to add - when I delete a few components I finish with a problem described here. |
Are you guys saying that any 3rd party library that has "@angular/core":"^2.0.0" (as an example) in it's package.json won't work when imported as a module? This is a nightmare, none of my 3rd party libraries work if this is the case. |
Never mind. My issue was related to the DLLReferencePlugin in Webpack. I had to clean my dll directory and rebuild them. |
You can also get this error if you declare a component in the imports array. |
Maybe you have "node_modules" directory in your sub directories! |
Hello! I have the same problem, I forked a library to make it work with angular cli aot, but no luck.
|
I'm facing a similar issue. However, i get this error when i try to register a service on my Angular4 app. The error i see strangely is for another component (which works fine when the service is removed) :/ |
I ran into this using angular-cli and after quite a bit of digging found that it had to do with the generated import 'core-js/es6/reflect';
import 'core-js/es7/reflect'; I commented those out and imported import 'reflect-metadata'; Things started working just fine after. Not exactly sure why this works... my understanding was that |
I was searching all over for tips on this error. Turns out I forgot to add "typings":"index.d.ts" to my package.json file |
I was having this error which turned out to be as @Lavaei suggested. I was doing local development on my shared module in a different project, and used As soon as I deleted Is it possible to tell Angular CLI to ignore these |
Can confirm @irontoby |
I've tried everything mentioned in this issue to no avail. Using the latest Angular packages (
This was all working fine before. |
@linktothapast Unfortunately this is a very sneaky error that can be caused by different reasons. The last time I experienced this when I was adding a module to the root module and it had BrowserModule and BrowserAnimationsModule imported in it and this was my mistake. At least be sure that two above mentioned modules imported only once and only in the root module, otherwise you can have errors that are not very descriptive. Use CommonModule in your other modules instead of BrowserModule. In my case I had the same error as you and it was about the module that was totally fine. I hope it might be helpful. Also please check your imports, declarations, exports that everything on its place. I mean there is not a case that you declared a module in declarations or imported service etc. These kinds of mistake might cause weird errors as well. |
Similar to @linktothapast I have tried everything I can think of. I've created 2 simple angular 5 projects, one to package up, and one to consume. I use |
@benelliott yep, I've been waiting for a solution for months now and this kind of stuff is a blocker to working with Angular. While I love promoting Angular for larger web dev teams because at its core it is a good framework, all these infrastructure issues and all the work that the Angular team has taken on is leading to lots of time wasted debugging issues like this. |
I am having this problem as well. We will be stuck on version 1.2.7 of the CLI until a solution is available. Note that this issue is particularly challenging for anyone on NPM 5 as it links projects by default that are specified via "file:" in package.json. The only way to make it work is to remove node_modules from my linked modules... which is not viable within our dev workflow. |
This is a closed ticket; not sure the devs look at replies to closed tickets. Maybe time to open a new ticket? Also, I wanted to point out that, at least for me, I found and mirrored setting up my modules as demonstrated in the ngx-rocket/starter-kit and it has worked well ever since. The only gotcha is when my IDE decides to use the shortcut notation (e.g., @shared/whatever) inside of a module when it shouldn't. |
Same issue here. Had a library written in
I am consuming the library via npm repo (no links involved, like a few comments above pointed out). It was all working fine when the lib was built using 4.x and consumed via 5.x. Now when the lib is built wit 5.x and used via 5.x, it breaks runtime with the following error.
The consuming project is using CLI 1.5. |
i have similar problem with dir structure:
reproduce steps:
start demo
messy workaroundremoving path mapping inside
library dependecies:
demo dependencies:
any idea? |
I also met this problem: same code works on Windows, but not works on Linux. This is my version infomation: windows
linux
Then I updated the |
Had the same problem - turned out the ng-pdf-make project had a subdirectory of node_modules with its own angular version which caused the bug. After deleting the local node_modules it worked! |
I also faced the same issue but resolved by updating the tsconfig.app.json with following code: { Its worked for me try it from your end. |
I am also facing same issueUncaught Error: Unexpected value 'Http' imported by the module 'AppModule'. Please add a @NgModule annotation. |
i used flash-message new version then i have this error in browser [WDS] Disconnected! This is app.module.ts // Required Fields
|
@Jayawardhana This is not a support group. Please post your question on Stack Overflow. And it does make sense, you do not declare a module in a module, you import it. |
Unhelpful and negative. |
It's not negative, it's a fact. Hijacking an issue with a "fix this code please" only creates noise for everyone. I've not only pointed the user in the right direction (StackOverflow), I've actually given a strong hint towards the solution:
So there's nothing negative about it and there's for sure a few things helpful there. |
Got the same error with angular v7 with an in-project library created with angular-cli. The cause in my case was that the components and modules were exported in public_api.ts via intermediate index.ts files. Removing index.ts files and exporting everything directly in public_api.ts solved this. Pretty sure there is a good technical explanation for this, but from the perspective of a simple "user" this belongs to the category of "insane"! |
Thanks! This solved the problem for me as well. The error message you get is remarkably unhelpful. |
this one save me. |
Thanks! |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
I'm submitting a ... (check one with "x")
Current behavior
I have an Angular application which imports a module from another repository.
So my app.module.ts
and then in my node_modules I have my common repository (installed with yarn/npm in package.json) with the file file my-common-module.d.ts
This works fine with Angular 2.
Now, I delete node_modules and change the version of Angular to 4.0 in the package.json of my app and then yarn again.
I build the app with webpack, no errors, and when I run it I get the following error in the browser
Expected behavior
I'd expect this to work the same with Angular4 or Angular2.
My question is, do I need to also upgrade the common module to Angular4 for this to work? I thought that this would already be transpiled to js when I import it in my main app module, maybe I'm wrong.
Please tell us about your environment:
I tested this with webpack-dev-server
Angular version: 2.0.X
With 2.0.0 it's working, with 4.0.0 it's not.
Browser:
I tested this in Chrome and FF.
Language:
I'm using typescript 2.2.2
Node (for AoT issues):
node --version
=The text was updated successfully, but these errors were encountered: