Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

233
Views
Angular 9 SSR - where to set global['window'] (using domino)?

After upgrading to Angular 9, I'm getting error ReferenceError: window is not defined when running yarn serve:ssr.

In our Angular apps, we use trick with Domino to emulate window for SSR (as in https://github.com/Angular-RU/angular-universal-starter/blob/master/server.ts#L21 ).

So after ng update I have added these lines after imports in server.ts:


const distFolder = join(process.cwd(), 'dist/browser');
const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index';

// Emulate browser APIs
const domino = require('domino');
const fs = require('fs');
const templateA = fs.readFileSync(join(distFolder, indexHtml)).toString();

const win = domino.createWindow(templateA);
win.Object = Object;
win.Math = Math;
global['window'] = win;
global['document'] = win.document;

However, it seems that setting global['window'] is happening either too late or never.

Do you have any idea where to set global['window'] so the angular components and libraries can access it in SSR?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Solved, thanks to this comment https://github.com/angular/universal/issues/1678#issuecomment-627031128

The key is to have line

import { AppServerModule } from './src/main.server';

after the definition of global['window']

Beware the autoformatters in your editor, which usually put the import lines on top of the file automatically :)

over 4 years ago · Santiago Trujillo Report

0

I put it below the imports and above the app() method


import { ngExpressEngine } from '@nguniversal/express-engine';
import * as express from 'express';
import { join } from 'path';

import { AppServerModule } from './src/main.server';
import { APP_BASE_HREF } from '@angular/common';
import { existsSync, readFileSync } from 'fs';
import * as domino from 'domino';
import * as core from 'express-serve-static-core';


const template = join('browser', 'index.html');
const win = domino.createWindow(template);

global['window'] = win;
global['document'] = win.document;

// The Express app is exported so that it can be used by serverless Functions.
export function app(): core.Express {
over 4 years ago · Santiago Trujillo Report

0

I do two tasks to fix it.

first - in angular.json set "optimization": false for server build, its helps find 3d paty module what calls error

second - i move up domino in my server.ts

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!