Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NG Universal app failed to start #2008

Closed
1 of 2 tasks
boskiv opened this issue Oct 8, 2019 · 13 comments · Fixed by #2057
Closed
1 of 2 tasks

NG Universal app failed to start #2008

boskiv opened this issue Oct 8, 2019 · 13 comments · Fixed by #2057

Comments

@boskiv
Copy link

boskiv commented Oct 8, 2019

Issue type

I'm submitting a ... (check one with "x")

  • bug report
  • feature request

Issue description

Current behavior:

After adding a universal module to ng, the server failed to start
Expected behavior:

Starts fine
Steps to reproduce:

ng new uniapp -S
cd uniapp
ng add @nebular/theme
ng add @nestjs/ng-universal

Related code:
Sample app: https://github.com/boskiv/nebular-universal

[nodemon] starting `node dist/server-app/main.js`
/Users/i_skiridomov/Documents/Projects/koko/universal-demo/node_modules/intersection-observer/intersection-observer.js:724
}(window, document));
  ^

ReferenceError: window is not defined
    at Object.<anonymous> (/Users/i_skiridomov/Documents/Projects/koko/universal-demo/node_modules/intersection-observer/intersection-observer.js:724:3)
    at Module._compile (internal/modules/cjs/loader.js:945:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:962:10)
    at Module.load (internal/modules/cjs/loader.js:798:32)
    at Function.Module._load (internal/modules/cjs/loader.js:711:12)
    at Module.require (internal/modules/cjs/loader.js:838:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at /Users/i_skiridomov/Documents/Projects/koko/universal-demo/node_modules/@nebular/theme/bundles/theme.umd.js:2:469
    at Object.<anonymous> (/Users/i_skiridomov/Documents/Projects/koko/universal-demo/node_modules/@nebular/theme/bundles/theme.umd.js:5:2)
    at Module._compile (internal/modules/cjs/loader.js:945:30)
[nodemon] app crashed - waiting for file changes before starting...

Other information:

npm, node, OS, Browser

➜  universal-demo git:(master) node --version
v12.11.1
➜  universal-demo git:(master) npm --version
6.11.3
➜  universal-demo git:(master)

Angular, Nebular

"@angular/common": "8.2.9"
"@nestjs/common": "6.8.2",
"@nebular/theme": "4.4.0",

@tayambamwanza
Copy link

I just got this last night as well

@niklasravnsborg
Copy link

I also have the same problem :/

@beshad
Copy link

beshad commented Oct 14, 2019

same problem here, backend is NestJS
ReferenceError: window is not defined at Object.<anonymous> (fullstack-nestjs-angular-boiletplate/node_modules/intersection-observer/intersection-observer.js:724:3)

@beshad
Copy link

beshad commented Oct 15, 2019

my problem was the SSR using NESTJS, solved it by adding below to my app.module.ts in NestJS. basically mimicking the windows object

const domino = require('domino')
const fs = require('fs')
const template = fs.readFileSync(join(BROWSER_DIR , 'index.html')).toString()
const win = domino.createWindow(template)

global['window'] = win
global['Node'] = win.Node
global['navigator'] = win.navigator
global['Event'] = win.Event
global['KeyboardEvent'] = win.Event
global['MouseEvent'] = win.Event
global['Event']['prototype'] = win.Event.prototype
global['document'] = win.document

@tayambamwanza
Copy link

@beshad Which file did you as this to, can you provide a sample? I tried using applyDomino() nest is utility in app.module for the server and it didn't work.

@beshad
Copy link

beshad commented Oct 15, 2019

@beshad Which file did you as this to, can you provide a sample? I tried using applyDomino() nest is utility in app.module for the server and it didn't work.

@tayambamwanza no that didn't work for me either, add this code to the app.module.ts:

const BROWSER_DIR = join(process.cwd(), 'dist/browser')

const domino = require('domino')
const fs = require('fs')
const template = fs.readFileSync(join(BROWSER_DIR , 'index.html')).toString()
const win = domino.createWindow(template)

global['window'] = win
global['Node'] = win.Node
global['navigator'] = win.navigator
global['Event'] = win.Event
global['KeyboardEvent'] = win.Event
global['MouseEvent'] = win.Event
global['Event']['prototype'] = win.Event.prototype
global['document'] = win.document

and then have a viewsPath in AngularUniverseModule:

@Module({
  imports: [
    AngularUniversalModule.forRoot({
      bundle: require('../server/main'),
      liveReload: true,
      viewsPath: BROWSER_DIR
    }),
    MongooseModule.forRoot('mongodb://localhost/fullstack-nestjs-angular-boilerplate',
      {
        useNewUrlParser: true,
        useUnifiedTopology: true
      }),
    UserModule,
    AuthModule
  ],
  controllers: [],
  providers: []
})
export class ApplicationModule { }

my boiletplate project is here: https://github.com/beshad/fullstack-nestjs-angular-boilerplate

@tayambamwanza
Copy link

@beshad I actually replaced nb with angular material because I was on a deadline, next project I'll try Nebular again and let you know.

@boskiv
Copy link
Author

boskiv commented Nov 25, 2019

Does not work with latest versions

├─ @nestjs/common@6.10.1
├─ @nestjs/core@6.10.1
├─ @nestjs/ng-universal@2.0.1
|─ @nebular/eva-icons@4.5.0
├─ @nebular/theme@4.5.0
$ node dist/server
/Users/i_skiridomov/Projects/koko/koko-nebular/node_modules/intersection-observer/intersection-observer.js:724
}(window, document));
  ^

ReferenceError: window is not defined
    at Object.<anonymous> (/Users/i_skiridomov/Projects/koko/koko-nebular/node_modules/intersection-observer/intersection-observer.js:724:3)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Function.Module._load (internal/modules/cjs/loader.js:727:14)
    at Module.require (internal/modules/cjs/loader.js:852:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at /Users/i_skiridomov/Projects/koko/koko-nebular/node_modules/@nebular/theme/bundles/theme.umd.js:2:469
    at Object.<anonymous> (/Users/i_skiridomov/Projects/koko/koko-nebular/node_modules/@nebular/theme/bundles/theme.umd.js:5:2)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

@boskiv
Copy link
Author

boskiv commented Nov 25, 2019

Works after app.module.ts update

import { Module } from '@nestjs/common';
import { AngularUniversalModule } from '@nestjs/ng-universal';
import { join } from 'path';

const BROWSER_DIR = join(process.cwd(), 'dist/browser')

const domino = require('domino')
const fs = require('fs')
const template = fs.readFileSync(join(BROWSER_DIR , 'index.html')).toString()
const win = domino.createWindow(template)

global['window'] = win
global['Node'] = win.Node
global['navigator'] = win.navigator
global['Event'] = win.Event
global['KeyboardEvent'] = win.Event
global['MouseEvent'] = win.Event
global['Event']['prototype'] = win.Event.prototype
global['document'] = win.document

@Module({
  imports: [
    AngularUniversalModule.forRoot({
      viewsPath: BROWSER_DIR,
      bundle: require('../server/main'),
      liveReload: true
    })
  ]
})
export class ApplicationModule {}

@anvial
Copy link

anvial commented Jul 22, 2020

Fellows,

After modifying server/app.module.ts, I've another error:

ERROR NAMESPACE_ERR: NAMESPACE_ERR (14): the operation is not allowed by Namespaces in XML

Anybody face with this?

@danieldanielecki
Copy link

Works after app.module.ts update

import { Module } from '@nestjs/common';
import { AngularUniversalModule } from '@nestjs/ng-universal';
import { join } from 'path';

const BROWSER_DIR = join(process.cwd(), 'dist/browser')

const domino = require('domino')
const fs = require('fs')
const template = fs.readFileSync(join(BROWSER_DIR , 'index.html')).toString()
const win = domino.createWindow(template)

global['window'] = win
global['Node'] = win.Node
global['navigator'] = win.navigator
global['Event'] = win.Event
global['KeyboardEvent'] = win.Event
global['MouseEvent'] = win.Event
global['Event']['prototype'] = win.Event.prototype
global['document'] = win.document

@Module({
  imports: [
    AngularUniversalModule.forRoot({
      viewsPath: BROWSER_DIR,
      bundle: require('../server/main'),
      liveReload: true
    })
  ]
})
export class ApplicationModule {}

The listing doesn't work for me on Angular 10 and @nestjs/ng-universal version 4.0.1 (latest). I can see that your app.module.ts has the bundle and that the Angular in that project was 7.2.X.

@boskiv @tayambamwanza were you able to get rid of this error after the recent updates in ng-universal and the fact that bundle is no longer required?

@boskiv
Copy link
Author

boskiv commented Oct 19, 2020

I've repeated steps with recent versions. Look likes it starts now.

➜ yarn serve:ssr
yarn run v1.22.10
$ node dist/uniapp/server/main.js
(node:82461) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
[Nest] 82461   - 10/19/2020, 2:31:28 PM   [NestFactory] Starting Nest application...
[Nest] 82461   - 10/19/2020, 2:31:28 PM   [InstanceLoader] AppModule dependencies initialized +20ms
[Nest] 82461   - 10/19/2020, 2:31:28 PM   [InstanceLoader] AngularUniversalModule dependencies initialized +1ms
[Nest] 82461   - 10/19/2020, 2:31:28 PM   [NestApplication] Nest application successfully started +5ms

@danieldanielecki
Copy link

I've repeated steps with recent versions. Look likes it starts now.

➜ yarn serve:ssr
yarn run v1.22.10
$ node dist/uniapp/server/main.js
(node:82461) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
[Nest] 82461   - 10/19/2020, 2:31:28 PM   [NestFactory] Starting Nest application...
[Nest] 82461   - 10/19/2020, 2:31:28 PM   [InstanceLoader] AppModule dependencies initialized +20ms
[Nest] 82461   - 10/19/2020, 2:31:28 PM   [InstanceLoader] AngularUniversalModule dependencies initialized +1ms
[Nest] 82461   - 10/19/2020, 2:31:28 PM   [NestApplication] Nest application successfully started +5ms

@boskiv thanks, I guess my issue is slightly different and posted it on StackOverflow: https://stackoverflow.com/questions/64458225/angular-universal-external-dependencies-issues-webpack-server-config-js-nodeex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants