Questions

window is not defined in angular universal?

Spread the love

At server side need to render JavaScript code for that you have to install a mock browser package.

npm install domino

The below code is at the top of the server.ts file

  const domino = require('domino');
  const win = domino.createWindow(indexHtml);
  

  global['window'] = win;
  global['Node'] = win.Node;
  global['navigator'] = win.navigator;
  global['Event'] = win.Event;
  global['KeyboardEvent'] = win.Event;
  global['MouseEvent'] = win.Event;
  global['Event']['prototype'] = win.Event.prototype;
  global['document'] = win.document;

For the above same thing you can do by different package command below

npm install mock-browser --save

The below code is at the top of the server.ts file

const MockBrowser = require('mock-browser').mocks.MockBrowser;
const mock = new MockBrowser();

global['document'] = mock.getDocument();
global['window'] = mock.getWindow();
import mock-browser to solve the problem - window is not defined in angular universal
import mock-browser to solve the problem – window is not defined in angular universal

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

Hope your all the windows is not define error have been solved now. Please give your valuable feedback. If you have any kind of questions or issues about this article, please let me know.


Spread the love
Tagged , , , , , , , ,

About Chandra

Technology lover. Professionally Software Developer and write some Technical Blog to help newcomer.
View all posts by Chandra →

9 thoughts on “window is not defined in angular universal?

  1. No its not resolved the error still getting same Error
    C:\Users\AKumarX0104902\Desktop\ClientApp\dist\ClientApp-server\main.js:222631
    !function(e,t){if(true)module.exports=t(__webpack_require__(/*! @ag-grid-community/core */ “./node_modules/@ag-grid-community/core/dist/cjs/main.js”));else { var r, n; }}(window,(function(e){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){“undefined”!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:”Module”}),Object.defineProperty(e,”__esModule”,{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&”object”==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,”default”,{enumerable:!0,value:e}),2&t&&”string”!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&

    ReferenceError: window is not defined
    at Object../node_modules/ag-grid-autocomplete-editor/ag-grid-autocomplete-editor.js (C:\Users\AKumarX0104902\Desktop\ClientApp\dist\ClientApp-server\main.js:222631:172)
    at __webpack_require__ (C:\Users\AKumarX0104902\Desktop\ClientApp\dist\ClientApp-server\main.js:26:30)
    at Module../src/app/shared/components/custom-modal/custom-modal.component.ts (C:\Users\AKumarX0104902\Desktop\ClientApp\dist\ClientApp-server\main.js:541653:85)
    at __webpack_require__ (C:\Users\AKumarX0104902\Desktop\ClientApp\dist\ClientApp-server\main.js:26:30)
    at Module../src/app/shared/components/custom-modal/custom-modal.component.ngfactory.js (C:\Users\AKumarX0104902\Desktop\ClientApp\dist\ClientApp-server\main.js:541575:81)
    at __webpack_require__ (C:\Users\AKumarX0104902\Desktop\ClientApp\dist\ClientApp-server\main.js:26:30)
    at Module../src/app/app.server.module.ngfactory.js (C:\Users\AKumarX0104902\Desktop\ClientApp\dist\ClientApp-server\main.js:530601:122)
    at __webpack_require__ (C:\Users\AKumarX0104902\Desktop\ClientApp\dist\ClientApp-server\main.js:26:30)
    at Module../server.ts (C:\Users\AKumarX0104902\Desktop\ClientApp\dist\ClientApp-server\main.js:530248:94)
    at __webpack_require__ (C:\Users\AKumarX0104902\Desktop\ClientApp\dist\ClientApp-server\main.js:26:30)

  2. that list below is for common window is not defined

    you might have been using different event except the list below please add for your different global event variable below with that

    global[‘window’] = win;
    global[‘Node’] = win.Node;
    global[‘navigator’] = win.navigator;
    global[‘Event’] = win.Event;
    global[‘KeyboardEvent’] = win.Event;
    global[‘MouseEvent’] = win.Event;
    global[‘Event’][‘prototype’] = win.Event.prototype;
    global[‘document’] = win.document;

    1. I am hitting error for window undefined and document undefined.
      After i did this it become ‘apply’ of undefined

      ERROR TypeError: Cannot read property ‘apply’ of undefined
      at __WEBPACK_DEFAULT_EXPORT__ (C:\Users\Desktop-Daaa1\Desktop\Project\playbook-fe\dist\playbook\server\791.js:117:19)
      at CardItemComponent.ngAfterViewInit (C:\Users\Desktop-Daaa1\Desktop\Project\playbook-fe\dist\playbook\server\791.js:228:79)
      at callHook (C:\Users\Desktop-Daaa1\Desktop\Project\playbook-fe\dist\playbook\server\main.js:48157:22)
      at callHooks (C:\Users\Desktop-Daaa1\Desktop\Project\playbook-fe\dist\playbook\server\main.js:48126:17)
      at executeInitAndCheckHooks (C:\Users\Desktop-Daaa1\Desktop\Project\playbook-fe\dist\playbook\server\main.js:48077:9)
      at refreshView (C:\Users\Desktop-Daaa1\Desktop\Project\playbook-fe\dist\playbook\server\main.js:55147:21)
      at refreshEmbeddedViews (C:\Users\Desktop-Daaa1\Desktop\Project\playbook-fe\dist\playbook\server\main.js:56201:17)
      at refreshView (C:\Users\Desktop-Daaa1\Desktop\Project\playbook-fe\dist\playbook\server\main.js:55100:9)
      at refreshComponent (C:\Users\Desktop-Daaa1\Desktop\Project\playbook-fe\dist\playbook\server\main.js:56247:13)
      at refreshChildComponents (C:\Users\Desktop-Daaa1\Desktop\Project\playbook-fe\dist\playbook\server\main.js:54873:9)

      Any idea of it?

Leave a Reply

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