Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

How to resolve window is not defined on npm run serve:ssr (server Node.js) #1835

Closed
Nazuk0 opened this issue Sep 17, 2020 · 14 comments
Closed

Comments

@Nazuk0
Copy link

Nazuk0 commented Sep 17, 2020

🐞 bug report

Affected Package

The issue is caused by package webpack, I think but not sure.

Description

The command line : "npm run serve:ssr" occurred the exception error : "widow is not defined"

🔥 Exception or Error


ReferenceError: window is not defined
    at /Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:1548072
    at Object.4R65 (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:1727030)
    at __webpack_require__ (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:317)
    at Module.2lPn (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:1227038)
    at __webpack_require__ (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:317)
    at Object.XmR9 (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:8620321)
    at __webpack_require__ (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:317)
    at Object.JcKL (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:6520553)
    at __webpack_require__ (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:317)
    at Object.V7fC (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:8196953)


🌍 Your Environment

Angular Version:


Angular CLI: 9.1.12
Node: 12.14.1
OS: darwin x64

Angular: 9.1.12
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, localize, platform-browser
... platform-browser-dynamic, platform-server, router
Ivy Workspace: No

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.901.12
@angular-devkit/build-angular     0.901.12
@angular-devkit/build-optimizer   0.901.12
@angular-devkit/build-webpack     0.901.12
@angular-devkit/core              9.1.12
@angular-devkit/schematics        9.1.12
@angular/cdk                      9.2.4
@angular/material                 9.2.4
@ngtools/webpack                  9.1.12
@nguniversal/builders             9.1.1
@nguniversal/common               9.1.1
@nguniversal/express-engine       9.1.1
@schematics/angular               9.1.12
@schematics/update                0.901.12
rxjs                              6.6.3
typescript                        3.8.3
webpack                           4.42.0


Anything else relevant?
I already try to add "domino" or "if (window !== undefined) but it is still not working.
Packages of Angular Universal was successfully installed and created all files we need.
npm run build:ssr occurred no problems.

@alan-agius4 alan-agius4 transferred this issue from angular/angular Sep 17, 2020
@alan-agius4
Copy link
Collaborator

Kindly see the below link, there is an entire section dedicated to this error.

https://github.com/angular/universal/blob/master/docs/gotchas.md

If the problem persists please share your server.ts.

@Nazuk0
Copy link
Author

Nazuk0 commented Sep 18, 2020 via email

@Nazuk0
Copy link
Author

Nazuk0 commented Sep 18, 2020 via email

@jasonburrows
Copy link

jasonburrows commented Sep 18, 2020

@Nazukai

Check out this other bug as I think it addresses the issues you are having and I've provided some specific code for how I fixed it on my side. If this isn't what you are looking for, please excuse me:

#1675

For me I had to both provide shims for the window stuff (for which I used domino) and shims for the components that were not behaving properly server side. The linked bug above will show both.

@mraath
Copy link

mraath commented Apr 20, 2021

HI @Nazukai

I have the EXACT same issue, how did you get this resolved?
Any help will be great. Thanks

@mraath
Copy link

mraath commented Apr 20, 2021

🐞 bug report

Affected Package

The issue is caused by package webpack, I think but not sure.

Description

The command line : "npm run serve:ssr" occurred the exception error : "widow is not defined"

🔥 Exception or Error


ReferenceError: window is not defined
    at /Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:1548072
    at Object.4R65 (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:1727030)
    at __webpack_require__ (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:317)
    at Module.2lPn (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:1227038)
    at __webpack_require__ (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:317)
    at Object.XmR9 (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:8620321)
    at __webpack_require__ (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:317)
    at Object.JcKL (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:6520553)
    at __webpack_require__ (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:317)
    at Object.V7fC (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:8196953)

🌍 Your Environment

Angular Version:


Angular CLI: 9.1.12
Node: 12.14.1
OS: darwin x64

Angular: 9.1.12
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, localize, platform-browser
... platform-browser-dynamic, platform-server, router
Ivy Workspace: No

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.901.12
@angular-devkit/build-angular     0.901.12
@angular-devkit/build-optimizer   0.901.12
@angular-devkit/build-webpack     0.901.12
@angular-devkit/core              9.1.12
@angular-devkit/schematics        9.1.12
@angular/cdk                      9.2.4
@angular/material                 9.2.4
@ngtools/webpack                  9.1.12
@nguniversal/builders             9.1.1
@nguniversal/common               9.1.1
@nguniversal/express-engine       9.1.1
@schematics/angular               9.1.12
@schematics/update                0.901.12
rxjs                              6.6.3
typescript                        3.8.3
webpack                           4.42.0

Anything else relevant?
I already try to add "domino" or "if (window !== undefined) but it is still not working.
Packages of Angular Universal was successfully installed and created all files we need.
npm run build:ssr occurred no problems.

What was the resolution? Thanks again

@Nazuk0
Copy link
Author

Nazuk0 commented Apr 20, 2021 via email

@mraath
Copy link

mraath commented Apr 21, 2021

Hi, I did not resolve it, because it was on my intership but i finished it. Sorry I can’t help you … Hope you will solve it Nazuko Provenance : Courrierhttps://go.microsoft.com/fwlink/?LinkId=550986 pour Windows 10 De : @.> Envoyé le :mardi 20 avril 2021 22:11 À : @.> Cc : @.>; @.> Objet :Re: [angular/universal] How to resolve window is not defined on npm run serve:ssr (server Node.js) (#1835) 🐞 bug report Affected Package The issue is caused by package webpack, I think but not sure. Description The command line : "npm run serve:ssr" occurred the exception error : "widow is not defined" 🔥 Exception or Error ReferenceError: window is not defined at /Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:1548072 at Object.4R65 (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:1727030) at webpack_require (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:317) at Module.2lPn (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:1227038) at webpack_require (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:317) at Object.XmR9 (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:8620321) at webpack_require (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:317) at Object.JcKL (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:6520553) at webpack_require (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:317) at Object.V7fC (/Users/nico/Desktop/Stage/CaptnBoat-SSR/captn-boat-front/dist/captn-boat-angular/server/main.js:1:8196953) 🌍 Your Environment Angular Version: Angular CLI: 9.1.12 Node: 12.14.1 OS: darwin x64 Angular: 9.1.12 ... animations, cli, common, compiler, compiler-cli, core, forms ... language-service, localize, platform-browser ... platform-browser-dynamic, platform-server, router Ivy Workspace: No Package Version

----------------------------------------------------------- @angular-devkit/architect 0.901.12 @angular-devkit/build-angular 0.901.12 @angular-devkit/build-optimizer 0.901.12 @angular-devkit/build-webpack 0.901.12 @angular-devkit/core 9.1.12 @angular-devkit/schematics 9.1.12 @angular/cdk 9.2.4 @angular/material 9.2.4 @ngtools/webpack 9.1.12 @nguniversal/builders 9.1.1 @nguniversal/common 9.1.1 @nguniversal/express-engine 9.1.1 @schematics/angular 9.1.12 @schematics/update 0.901.12 rxjs 6.6.3 typescript 3.8.3 webpack 4.42.0 Anything else relevant? I already try to add "domino" or "if (window !== undefined) but it is still not working. Packages of Angular Universal was successfully installed and created all files we need. npm run build:ssr occurred no problems. What was the resolution? Thanks again — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub<#1835 (comment)>, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ALWEI2V5LVQUR7IELAOQPJLTJXNXBANCNFSM4RQQCQMQ.

Thanks for letting me know! I really appreciate this... Let's see if I get it resolved :-D

@mraath
Copy link

mraath commented Apr 21, 2021

Thanks @Nazuk0 - I appreciate your reply!

@mraath
Copy link

mraath commented Apr 21, 2021 via email

@hiepxanh
Copy link
Contributor

hiepxanh commented Mar 7, 2022

this is my ssr for angular 13. @Nazuk0 can you close this issue?

// STARTING: monkey patch window
import { createWindow } from 'domino';
import { join } from 'path';
import { existsSync } from 'fs';
import 'localstorage-polyfill';

const checkIndexPath = existsSync(join(process.cwd(), 'dist/apps/reader/web/index.html'));
const indexHtml = checkIndexPath ? join(process.cwd(), 'dist/apps/reader/web/index.html') : join(process.cwd(), 'web/index.html');

function startWindow() {

  // console.log('reseting windows.....');
  const win = createWindow(indexHtml);

  // Polyfills
  (global as any).window = win;
  (global as any).document = win.document;
  (global as any).navigator = win.navigator;
  (global as any).location = win.location;
  (global as any)['window']['isMobileOnServer'] = true;
  global['localStorage'] = localStorage;
}

startWindow();

@ohabash
Copy link

ohabash commented Mar 16, 2022

@hiepxanh where would that go?...

In old apps i used domino but that doesnt work for these kinds of bugs cant figure out why. Ive delt with this bug many times. This time in ng 13 is different

@ohabash
Copy link

ohabash commented Mar 16, 2022

I have been solving this bug for years. Why wont it work anymore.

// moc browser
const MockBrowser = mocks.MockBrowser;
const mock = new MockBrowser();
global['document'] = mock.getDocument();
global['navigator'] = mock.getNavigator();
global['window'] = mock.getWindow();
global['XMLHttpRequest'] = require('xmlhttprequest').XMLHttpRequest;

why does universal want me to loose my job!

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Apr 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants