NullInjectorError: No provider for SwUpdate! SwUpdate does not work Angular 5

After much research it seems like it was an issue with angular 5.2.1 so..

I upgraded my project to angular 7, ran ng add @angular/pwa and that solved my problem!


For the ones who are getting this error when trying to write unit tests the solution is to add the ServiceWorkerModule to the imports array with the enable to false option when configuring the TestBed:

TestBed.configureTestingModule({
  declarations: [ MyComponent ],
  imports: [
    ServiceWorkerModule.register('ngsw-worker.js', { enabled: false })
  ]
})
.compileComponents();

Your issue is in the app.module.ts.

You need to import your service worker like this:

imports: [
    ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production }),
],

The code you had before was only including the module when you were in production mode, so during development it wasn't including the module at all.

EDIT:

Inside my app.component.ts I have:

constructor(private swUpdate: SwUpdate) { }

ngAfterViewInit() {
  if (this.swUpdate.isEnabled) {
    this.swUpdate.available
      .subscribe(() => {
        this.swUpdate
          .activateUpdate()
          .then(() => {
            window.location.reload();
          });
      });
  }
}

EDIT:

As per your comment, updating to latest version of Angular fixed your issue, it's nice they've made upgrading so simple now :)