nestjs/ng-universal

window is not defined, even defining using domino

GlauberF opened this issue · 3 comments


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

I'm creating a mock for window with the domino package, but it doesn't recognize

Erro:


Compiled successfully.
/var/www/html/pwa_vimbo/dist/server/main.js:420213
})(window, function(__WEBPACK_EXTERNAL_MODULE__0__, __WEBPACK_EXTERNAL_MODULE__2__) {
   ^

ReferenceError: window is not defined
    at Object.bzdy (/var/www/html/pwa_vimbo/dist/server/main.js:420213:4)
    at __webpack_require__ (/var/www/html/pwa_vimbo/dist/server/main.js:20:30)
    at Object.Tg+X (/var/www/html/pwa_vimbo/dist/server/main.js:327717:34)
    at __webpack_require__ (/var/www/html/pwa_vimbo/dist/server/main.js:20:30)
    at Object.56sf (/var/www/html/pwa_vimbo/dist/server/main.js:114904:22)
    at __webpack_require__ (/var/www/html/pwa_vimbo/dist/server/main.js:20:30)
    at Object.ZAI4 (/var/www/html/pwa_vimbo/dist/server/main.js:388541:22)
    at __webpack_require__ (/var/www/html/pwa_vimbo/dist/server/main.js:20:30)
    at Object.24aS (/var/www/html/pwa_vimbo/dist/server/main.js:78613:22)
    at __webpack_require__ (/var/www/html/pwa_vimbo/dist/server/main.js:20:30)

A server error has occurred.
node exited with 1 code.
connect ECONNREFUSED 127.0.0.1:34295

Code server.ts


import 'zone.js/dist/zone-node';
import './server/main';

// Mock
const domino = require('domino');
const fs = require('fs');
const path = require('path');
const template = fs.readFileSync(path.join(__dirname.split('server')[0], 'browser', 'index.html')).toString();
const win = domino.createWindow(template);
global['window'] = win;
Object.defineProperty(win.document.body.style, 'transform', {
    value: () => {
        return {
            enumerable: true,
            configurable: true
        };
    },
});
global['document'] = win.document;
global['CSS'] = null;

export * from './src/main.server';

Environment


Angular CLI: 10.1.2
Node: 12.18.1
OS: linux x64

Angular: 10.1.2
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... platform-server, router, service-worker
Ivy Workspace: Yes

Package                            Version
------------------------------------------------------------
@angular-devkit/architect          0.1001.2
@angular-devkit/build-angular      0.1001.2
@angular-devkit/build-optimizer    0.1000.8
@angular-devkit/core               10.1.2
@angular-devkit/schematics         10.1.2
@angular/cdk                       10.2.2
@angular/flex-layout               10.0.0-beta.32
@angular/material                  10.2.2
@angular/material-moment-adapter   10.2.2
@nguniversal/builders              10.1.0
@nguniversal/express-engine        10.1.0
@schematics/angular                10.1.2
@schematics/update                 0.1001.2
rxjs                               6.6.3
typescript                         3.9.7

This issue isn't related to NestJS + Angular Universal integration.

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.

thanks

Definitely +1, explained more in depth in #163 (comment)