question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to fix “ReferenceError: window is not defined” error in angular 6 universal server.js

See original GitHub issue

I’m submitting a…


[X] Bug report  
[ ] Feature request
[ ] Documentation issue or request

In package


[ ] @ng-toolkit/init
[ ] @ng-toolkit/serverless
[X] @ng-toolkit/universal
[ ] @ng-toolkit/pwa
[ ] @ng-toolkit/firebug

Current behavior

I execute the following command on angular cli command line tool, to install universal package for angular6 ‘ng add @ng-toolkit/universal’. It installed good,after that i execute npm run build:prod and it also runs very well. Now finally I run ‘npm run server’.But this throws me error without giving me launching url. ReferenceError: window is not defined at Object.exports.__esModule (D:\ANGULAR2\dist\server.js:122614:14) at webpack_require (D:\ANGULAR2\dist\server.js:122314:31) at Object.exports.__esModule (D:\ANGULAR2\dist\server.js:122359:19) at webpack_require (D:\ANGULAR2\dist\server.js:122314:31) at Object.exports.__esModule (D:\ANGULAR2\dist\server.js:122343:21) at webpack_require (D:\ANGULAR2\dist\server.js:122314:31) at D:\ANGULAR2\dist\server.js:122334:19 at D:\ANGULAR2\dist\server.js:122337:11 at webpackUniversalModuleDefinition (D:\ANGULAR2\dist\server.js:122292:21) at Object.exports.__esModule (D:\ANGULAR2\dist\server.js:122294:4) npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! rssmtechwood@0.0.0 serverrl node local.js npm ERR! Exit status 1

Expected behavior

when I run this command npm run server, I expect the following lines rssmtechwood@0.0.0 server:D:\ANGULAR2\rssmtechwood node local.js Listening on http://localhost:8080

Minimal reproduction of the problem with instructions

Step 1: I execute ng add @ng-toolkit/universal in angular/cli command prompt. Note: It produces server.js file automatically in dist folder. Step 2: Executes npm run build:prod Step 3: Finally he executes npm run server.

Example repository

What is the motivation / use case for changing the behavior?

Environment


Angular CLI: 6.2.9
Angular version: 6.1.0


- Node version: 8.12  
- Platform:  Windows 8.1

Others:

IDE- Visual Studio Code

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14

github_iconTop GitHub Comments

1reaction
vahidvdncommented, Apr 25, 2019

@gitulaga Ok, you don’t have to run Amazon ads in server side.

The only benefit that server side rendering brings to you, is being SEO friendly. In this case, it’s not important. Just skip it in server side and let Angular runs it in the client side.

For now you can close this issue.

1reaction
gitulagacommented, Apr 25, 2019

Thanks, Vahidvdn so much… Actually, it is amazon ads not working in my site, if I comment then angular universal is working. Amazon ads are called by using postscribe package in my typescript code. I don’t know why it is not supporting for angular6 universal

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to resolve window is not defined on npm run serve:ssr ...
... "npm run serve:ssr" occurred the exception error : "widow is not defined" Exception or Error ReferenceError: window is not defined at ...
Read more >
Angular Universal: ReferenceError: window is not defined
Angular Universal : ReferenceError: window is not defined ... This error can be caused by a reference to the Window object if you...
Read more >
ReferenceError: window is not defined Angular Universal
ReferenceError: window is not defined Angular Universal ... It's simple fix, ... projects/asfc-web/src/main.server';.
Read more >
window is not defined in angular universal? - SoftTech Diary
ReferenceError: window is not defined to re produce you need to run javascript code as run in server side by install mock browser....
Read more >
Error:- Window is not defined angular universal 8.2 - OpenVidu
... ReferenceError: window is not defined at new OpenViduLogger (/Users/userName/myApplication/projects/my-app/dist/server/main.js:71890:23) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found