-
Notifications
You must be signed in to change notification settings - Fork 482
How to resolve window is not defined on npm run serve:ssr (server Node.js) #1835
Comments
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. |
Hi Alan,
First, thanks a lot for your mail.
I just want to clarify something.
If I really understand the first strategy to fix my problem, I need to implement those injections in a service.ts and then when I use window.something in my code, I will replace them by the service, that right?
Best regards,
Nicolas
De : Alan Agius <[email protected]>
Répondre à : angular/universal <[email protected]>
Date : jeudi 17 septembre 2020 à 17:26
À : angular/universal <[email protected]>
Cc : Nazukai <[email protected]>, Author <[email protected]>
Objet : Re: [angular/universal] How to resolve window is not defined on npm run serve:ssr (server Node.js) (#1835)
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.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub<#1835 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/ALWEI2SYINAJV2J6HYTX7ITSGITBNANCNFSM4RQQCQMQ>.
|
Hi I think I really need some help to fix this error of window is not defined.
Here, you have a list of winow uses in my app :
* window.addEventListener
* window.matchMedia
* window.scrollTo
* window.getComputedStyle
* window.event
* window.responsiveNav
* window.open
* window.console
* window.setTimeout
* window.innerWidth
* window.innerHeight
And here, you have my all file of server.ts :
import 'zone.js/dist/zone-node';
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 } from 'fs';
// The Express app is exported so that it can be used by serverless Functions.
export function app() {
const server = express();
const distFolder = join(process.cwd(), 'dist/captn-boat-angular/browser');
const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index';
// Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
server.engine('html', ngExpressEngine({
bootstrap: AppServerModule,
}));
server.set('view engine', 'html');
server.set('views', distFolder);
// Example Express Rest API endpoints
// server.get('/api/**', (req, res) => { });
// Serve static files from /browser
server.get('*.*', express.static(distFolder, {
maxAge: '1y'
}));
// All regular routes use the Universal engine
server.get('*', (req, res) => {
res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
});
return server;
}
function run() {
const port = process.env.PORT || 4200;
// Start up the Node server
const server = app();
server.listen(port, () => {
console.log(`Node Express server listening on http://localhost:${port}`);
});
}
// Webpack will replace 'require' with '__webpack_require__'
// '__non_webpack_require__' is a proxy to Node 'require'
// The below code is to ensure that the server is run only when not requiring the bundle.
declare const __non_webpack_require__: NodeRequire;
const mainModule = __non_webpack_require__.main;
const moduleFilename = mainModule && mainModule.filename || '';
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
run();
}
export * from './src/main.server';
And to let you know, I also have a server.js who are attached to server.ts :
// Demarrage du service web
const express = require('express');
const app = express();
app.use(express.static(__dirname + '/dist/fr/'));
app.all('*',(req,res)=> {
res.status(200).sendFile(__dirname + '/dist/fr/index.html');
})
app.listen(process.env.PORT || 8080);
Thanks to your help !
Best regards,
Nicolas
De : Alan Agius <[email protected]>
Répondre à : angular/universal <[email protected]>
Date : jeudi 17 septembre 2020 à 17:26
À : angular/universal <[email protected]>
Cc : Nazukai <[email protected]>, Author <[email protected]>
Objet : Re: [angular/universal] How to resolve window is not defined on npm run serve:ssr (server Node.js) (#1835)
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.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub<#1835 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/ALWEI2SYINAJV2J6HYTX7ITSGITBNANCNFSM4RQQCQMQ>.
|
@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: For me I had to both provide shims for the |
HI @Nazukai I have the EXACT same issue, how did you get this resolved? |
What was the resolution? Thanks again |
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 : Courrier<https://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 unsubscribe<https://github.com/notifications/unsubscribe-auth/ALWEI2V5LVQUR7IELAOQPJLTJXNXBANCNFSM4RQQCQMQ>.
|
Thanks for letting me know! I really appreciate this... Let's see if I get it resolved :-D |
Thanks @Nazuk0 - I appreciate your reply! |
Thanks again for your help. Keep well!!
…On Tue, Apr 20, 2021 at 11:32 PM Nazuk0 ***@***.***> wrote:
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 : Courrier<https://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 unsubscribe<
https://github.com/notifications/unsubscribe-auth/ALWEI2V5LVQUR7IELAOQPJLTJXNXBANCNFSM4RQQCQMQ>.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#1835 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABBRCVO6FYB2YMFECW2Z3S3TJXXGJANCNFSM4RQQCQMQ>
.
|
this is my ssr for angular 13. @Nazuk0 can you close this issue?
|
@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 |
I have been solving this bug for years. Why wont it work anymore.
why does universal want me to loose my job! |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
🐞 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
🌍 Your Environment
Angular Version:
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.
The text was updated successfully, but these errors were encountered: