Angular 17: localStorage is not defined Error After Saving Changes and Browser Stuck in Reloading State

When i run project in angular 17 it runs successfully. However when i made some changes in code and save changes then:

The browser page stucks in a reloading state

Also this error shown up:

Error during ngOnInit: ReferenceError: localStorage is not defined

Here is my ngOnInit code block:

async ngOnInit() {
    try {
      await this.fetchUsers();
  
      this.chatService.fetchMessages();
      this.chatService.messages.subscribe((messages) => {
        this.messages = messages;
      });
  
      const users = this.authService.getUsers();
      const { username, id } = this.authService.getUserNameFromToken() || {};
      this.name = username ?? '';
      console.log("Display token name:", this.name);
      let token = localStorage.getItem('authToken');
      console.log("Token: ",token);
      this.id = id ?? '';
    } catch (error) {
      console.error("Error during ngOnInit:", error);
    }

  }
```[![Here is image of browser page which stucks in loading state when i made changes in code and save them][1]][1]


  [1]: https://i.sstatic.net/4jvdngLj.png