error NG8002: Can't bind to 'formGroup' since it isn't a known property of 'form'.
See original GitHub issueWhich @angular/* package(s) are the source of the bug?
Don’t known / other
Is this a regression?
No
Description
I’m getting following error
Error: projects/my-lib/src/lib/my-lib.component.html:1:7 - error NG8002: Can’t bind to ‘formGroup’ since it isn’t a known property of ‘form’.
After running ng test
command although I import ReactiveFormsModule in TestBed.configureTestingModule method.
Steps to reproduce:
- git clone https://github.com/arekgotfryd/minimal-reproduction
- cd minimal-reproduction
- npm i
- ng test
Please provide a link to a minimal reproduction of the bug
https://github.com/arekgotfryd/minimal-reproduction
Please provide the exception or error you saw
Error: projects/my-lib/src/lib/my-lib.component.html:1:7 - error NG8002: Can't bind to 'formGroup' since it isn't a known property of 'form'.
1 <form [formGroup]="linkingForm">
~~~~~~~~~~~~~~~~~~~~~~~~~
projects/my-lib/src/lib/my-lib.component.ts:6:16
6 templateUrl: './my-lib.component.html',
~~~~~~~~~~~~~~~~~~~~~~~~~
Error occurs in the template of component MyLibComponent.
Please provide the environment you discovered this bug in (run ng version
)
Angular CLI: 11.2.18
Node: 14.18.2
OS: win32 x64
Angular: 11.2.14
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router
Ivy Workspace: Yes
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1102.18
@angular-devkit/build-angular 0.1102.18
@angular-devkit/core 11.2.18
@angular-devkit/schematics 11.2.18
@angular/cli 11.2.18
@schematics/angular 11.2.18
@schematics/update 0.1102.18
ng-packagr 11.2.4
rxjs 6.6.7
typescript 4.1.6
webpack 4.19.1
Anything else?
Link to screenshot from terminal: Screenshot
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Can't bind to 'formGroup' since it isn't a known property of 'form'
RC6/RC7/Final release FIX. To fix this error, you just need to import ReactiveFormsModule from @angular/forms in your module.
Read more >Fixing Can't bind to 'formGroup' since it isn't a known property ...
Steps to fix Can't bind to formGroup since it isn't a known property of form error in Angular,1. Import FormsModule, ReactiveFormsModule ...
Read more >Can't Bind to formGroup Not Known Property Error in Angular
Angular is trying to tell us that it doesn't know about the formGroup directive on the <form> element in your component. This usually...
Read more >Can t bind to formGroup since it isn t a known property of form
The situation that I am in is that I have been trying to make what should be a very simple form ... that...
Read more >Can't bind to 'formGroup' since it isn't a known property of 'form'.
[solved] error NG8002: Can't bind to 'formGroup' since it isn't a known ... You just need to import ReactiveFormsModule from @angular/forms in your...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This happens because you’re using a custom Webpack configuration from within your Karma configuration file. The Webpack configuration registers
AngularCompilerPlugin
, which runs the AOT compiler on the production code. This is why you get type-checking of production code.In your spec compilation unit however, only
src/test.ts
and**/**.spec.ts
files are included (according totsconfig.spec.json
. Themy-lib.component.spec.ts
file importsmy-lib.component.ts
but that’s where the import chain ends; there is no import intomy-lib.module.ts
in any of the source files that are part of the testing program. Hence, regardless of whetherMyLibModule
actually importsReactiveFormsModule
or not the actualMyLibComponent
component does not have a corresponding NgModule that declares it, at least it’s not seen by the compiler.The testing module is entirely irrelevant here, as
TestBed
uses JIT compilation and testing modules are not visible to the AOT compiler.There are a couple of options to get this to work. I’d recommend that you move away from the custom Webpack configuration in your Karma setup, because without it the CLI will run your tests in JIT mode entirely.
With the custom Webpack config, you can pass
skipCodeGeneration: true
as an option forAngularCompilerPlugin
. Please be aware thatAngularCompilerPlugin
has been replaced withAngularWebpackPlugin
since v12 and the former is no longer available. I believe the new option would bejitMode: true
.Another alternative could be to add
src/public-api.ts
to thefiles
array intsconfig.spec.json
, as then you’ll find thatMyLibModule
becomes part of the compilation unit at which point it can importReactiveFormsModule
to fix the error (which is an actual problem in your production code; this import is vital for things to work).I’m going to close this since the issue is specific to your custom Webpack setup and this is otherwise working as expected.
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.