1 | initial version |
The "window is not defined" error occurs in Angular Server-Side Rendering (SSR) when a module or component tries to access the global "window" object, which is not available in the Node.js environment. Here are some ways to resolve this error:
if (typeof window !== 'undefined') { // Use window object methods or properties here }
import { Component, Inject } from '@angular/core'; import { WINDOW } from '@angular/common'; @Component({ selector: 'app-root', template: 'My app', }) export class AppComponent { constructor(@Inject(WINDOW) private window: Window) {} }
const { JSDOM } = require('jsdom'); const { window } = new JSDOM(); global.window = window; global.document = window.document;
The global
object makes the Window object available globally for SSR.