How To Solve Window Is Not Defined In Angular 11

In this article, we will learn how to solve window is not defined in Angular 11.

window is not defined error occurs when an angular application uses Angular Universal for server-side rendering.

If you want to run your angular application with server-side rendering, you can follow the link given below to implement server-side rendering in Angular.

Server-Side Rendering In Angular 11

As Angular – Server-side rendering (SSR) with Angular Universal documentation says:

Because a Universal app doesn’t execute in the browser, some of the browser APIs and capabilities may be missing on the server.
For example, server-side applications can’t reference browser-only global objects such as alert, window, document, navigator, or location.

Let’s follow the steps given below, to solve the window is not defined error.

Firstly execute the command given below to add a mock-browser package.

npm install mock-browser

Now open the server.ts file and add the code to it.

const MockBrowser = require('mock-browser').mocks.MockBrowser;
const mock = new MockBrowser();
global['window'] = mock.getWindow();

Rebuild your project with npm run build:ssr and serve with npm run serve:ssr.

Hope all window errors have been suppressed now. Please give your valuable feedback and if you have any questions or issues about this article, please let me know.

Also, check How To Solve LocalStorage Is Not Defined In Angular 11


  1. Kunjan Thakkar

    You guys are using npm package with window in your app. solution for that you have to use all package of window take all it locally

  2. gvccvn

    not working

  3. Mladen

    I still get the : Error: window is not defined

    And I also get:

    Warning: ./node_modules/mock-browser/node_modules/jsdom/lib/jsdom/utils.js 216:21-40
    Critical dependency: the request of a dependency is an expression
    at CommonJsRequireContextDependency.getWarnings (C:\Users\Mladen\code\frontend\node_modules\@angular-devkit\build-angular\node_modules\webpack\lib\dependencies\ContextDependency.js:40:18)
    at Compilation.reportDependencyErrorsAndWarnings (C:\Users\Mladen\code\frontend\node_modules\@angular-devkit\build-angular\node_modules\webpack\lib\Compilation.js:1454:24)
    at C:\Users\Mladen\code\frontend\node_modules\@angular-devkit\build-angular\node_modules\webpack\lib\Compilation.js:1258:10
    at _next0 (eval at create (C:\Users\Mladen\code\frontend\node_modules\tapable\lib\HookCodeFactory.js:33:10), :32:1)
    at eval (eval at create (C:\Users\Mladen\code\frontend\node_modules\tapable\lib\HookCodeFactory.js:33:10), :45:1)
    at runMicrotasks ()
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    @ ./node_modules/mock-browser/node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js
    @ ./node_modules/mock-browser/node_modules/jsdom/lib/jsdom/browser/Window.js
    @ ./node_modules/mock-browser/node_modules/jsdom/lib/jsdom.js
    @ ./node_modules/mock-browser/lib/MockBrowser.js
    @ ./node_modules/mock-browser/index.js
    @ ./server.ts
    @ multi ./server.ts

  4. Varis Memon

    I resolved error for window and lcal-storage , now i am getting same error for jQuery and few other words used in angular App.

    IS there any common solution for the same ?


Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories