universal: Angular 9: Window is not defined

Description

I’m trying to migrate my solution to the latest angular universal version from AU 8 and during execute npm run dev:ssr I get an error:

...\dist-server\main.js:193133
})(window, document, 'Hammer');
   ^

ReferenceError: window is not defined

In previous version of server.ts my collegue figure it out like below:

const domino = require('domino');
const fs = require('fs');
const path = require('path');
const template = fs.readFileSync(path.join(__dirname, '.', 'dist', 'index.html')).toString();
const win = domino.createWindow(template);
const files = fs.readdirSync(`${process.cwd()}/dist-server`);

global['window'] = win;
Object.defineProperty(win.document.body.style, 'transform', {
  value: () => {
    return {
      enumerable: true,
      configurable: true,
    };
  },
});
global['document'] = win.document;

How should I set β€˜window’ in the latest version of AU?

πŸ”₯ Exception or Error


frontend\universal\dist-server\main.js:193133
})(window, document, 'Hammer');
   ^

ReferenceError: window is not defined
    at Object../node_modules/hammerjs/hammer.js (K:\Projekty\Traceabilit frontend\universal\dist-server\main.js:193133:4)
    at __webpack_require__ (K:\Projekty\Traceabilit frontend\universal\dist-server\main.js:20:30)
    at Object../src/app/app.module.ts (K:\Projekty\Traceabilit frontend\universal\dist-server\main.js:239184:1)
    at __webpack_require__ (K:\Projekty\Traceabilit frontend\universal\dist-server\main.js:20:30)
    at Object../src/app/app.server.module.ts (K:\Projekty\Traceabilit frontend\universal\dist-server\main.js:239343:22)
    at __webpack_require__ (K:\Projekty\Traceabilit frontend\universal\dist-server\main.js:20:30)
    at Object../src/main.server.ts (K:\Projekty\Traceabilit frontend\universal\dist-server\main.js:252541:27)
    at __webpack_require__ (K:\Projekty\Traceabilit frontend\universal\dist-server\main.
js:20:30)
    at Object../server.ts (K:\Projekty\Traceabilit frontend\universal\dist-server\main.js:238941:23)
    at __webpack_require__ (K:\Projekty\Traceabilit frontend\universal\dist-server\main.js:20:30)

A server error has occurred.
node exited with 1 code.

🌍 Your Environment

β€œ@angular/animations”: β€œ^9.0.2”, β€œ@angular/cdk”: β€œ^9.0.1”, β€œ@angular/common”: β€œ^9.0.2”, β€œ@angular/compiler”: β€œ^9.0.2”, β€œ@angular/core”: β€œ^9.0.2”, β€œ@angular/forms”: β€œ^9.0.2”, β€œ@angular/material”: β€œ^9.0.1”, β€œ@angular/platform-browser”: β€œ^9.0.2”, β€œ@angular/platform-browser-dynamic”: β€œ^9.0.2”, β€œ@angular/pwa”: β€œ^0.900.3”, β€œ@angular/router”: β€œ^9.0.2”, β€œ@angular/service-worker”: β€œ^9.0.2”, β€œ@nguniversal/common”: β€œ^9.0.1”, β€œ@nguniversal/express-engine”: β€œ^9.0.1”,

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 29

Most upvoted comments

Problem was related to hammerjs, after changed it on @egjs/hammerjs solution is building