Error: Export of name 'ngModel' not found

18,850

Solution 1

import FormsModule in your respective spec.ts file and app.module.ts if not

beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports:[RouterTestingModule,HttpClientTestingModule,FormsModule],
      declarations: [ MyComponent ]
    })
    .compileComponents();
  }));

Solution 2

I had the same error (though in dev), it turns out that I had not added the FormsModule module to the app.module.ts file. See below:

enter image description here

It is also stated here along with a few more problems and their corresponding solutions

Solution 3

Another possible issue: if you're using template form validation When you set #name="ngModel" on a form field without [(ngModel)] directive

E.g.

<input required [value]="hero.name" (change)="setHeroName($event)" #name="ngModel" >

<div *ngIf="name.invalid && (name.dirty || name.touched)"
  class="alert alert-danger">Error</div>

=> Error: Export of name 'ngModel' not found

Fixed by replacing

[value]="hero.name" (change)="setHeroName($event)"

by

[(ngModel)]="hero.name"

Solution 4

You need to import FormsModule in your .spec.ts and app.module.ts (if necessary) file, as per below:

import { FormsModule } from '@angular/forms';    <------ Add this code

describe('Testing Component', () => {
  ...
  ...

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [ 
            FormsModule      <------ Add this code
        ]
    })
    .compileComponents();
  }));

  ...
  ...

Solution 5

I had a template variable called ngModel. Not sure what affect this had, but removing seemed to fix it.

Share:
18,850
izeko
Author by

izeko

Updated on June 27, 2022

Comments

  • izeko
    izeko almost 2 years

    After building my angular project i get the error: Error: Export of name 'ngModel' not found!

    I have my UI running in a docker container

    not even sure where to look for this. Its working fine in dev. (ng serve)

    Any ideas

  • Mahmoud
    Mahmoud over 3 years
    That fixed the issue.
  • Shivam Jha
    Shivam Jha about 3 years
    Welcome to Stack Overflow. See How do I write a good answer?
  • Robert
    Robert about 3 years
    Hey, I've formatted your code a bit, but both snippets look the same to me. Maybe I messed up during formatting? Please edit your question to fix as needed.