angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Error in SSR if server build with `bundleDependencies=all`

Open clark0x opened this issue 8 years ago • 19 comments

Bug Report or Feature Request (mark with an x)

- [x] bug report -> please search issues before submitting
- [ ] feature request

Command (mark with an x)

- [ ] new
- [ ] build
- [ ] serve
- [ ] test
- [ ] e2e
- [ ] generate
- [ ] add
- [ ] update
- [ ] lint
- [ ] xi18n
- [x] run
- [ ] config
- [ ] help
- [ ] version
- [ ] doc

Versions

Angular CLI: 6.2.1 Node: 8.11.2 OS: darwin x64 Angular: 6.1.7 ... animations, common, compiler, compiler-cli, core, forms ... http, language-service, platform-browser ... platform-browser-dynamic, platform-server, router ... service-worker

Package Version

@angular-devkit/architect 0.8.1 @angular-devkit/build-angular 0.8.1 @angular-devkit/build-ng-packagr 0.8.1 @angular-devkit/build-optimizer 0.8.1 @angular-devkit/build-webpack 0.8.1 @angular-devkit/core 0.8.1 @angular-devkit/schematics 0.8.1 @angular/cdk 6.4.7 @angular/cli 6.2.1 @angular/flex-layout 6.0.0-beta.18 @angular/material 6.4.7 @angular/pwa 0.8.1 @ngtools/json-schema 1.1.0 @ngtools/webpack 6.2.1 @schematics/angular 0.8.1 @schematics/update 0.8.1 ng-packagr 4.1.1 rxjs 6.3.2 typescript 2.9.2 webpack 4.18.0

Repro steps

I use universal-starter, only change one line in angular.json:

"server": {
  "configurations": {
    "production": {
      "bundleDependencies": "all", <= added
      ...
$ git clone -b issue-error-with-bundleDependencies  [email protected]:clarkorz/universal-starter.git
$ cd universal-starter
$ yarn install
$ yarn build:ssr
$ yarn serve:ssr

Access http://localhost:4000 in browser.

The log given by the failure

Error: No NgModule metadata found for '[object Object]'.
    at NgModuleResolver.resolve (/Users/wangshuo/Documents/GitHub/universal-starter/node_modules/@angular/compiler/bundles/compiler.umd.js:11962:27)
    at CompileMetadataResolver.getNgModuleMetadata (/Users/wangshuo/Documents/GitHub/universal-starter/node_modules/@angular/compiler/bundles/compiler.umd.js:10604:47)
    at JitCompiler._loadModules (/Users/wangshuo/Documents/GitHub/universal-starter/node_modules/@angular/compiler/bundles/compiler.umd.js:23914:55)
    at JitCompiler._compileModuleAndComponents (/Users/wangshuo/Documents/GitHub/universal-starter/node_modules/@angular/compiler/bundles/compiler.umd.js:23895:40)
    at JitCompiler.compileModuleAsync (/Users/wangshuo/Documents/GitHub/universal-starter/node_modules/@angular/compiler/bundles/compiler.umd.js:23855:41)
    at CompilerImpl.compileModuleAsync (/Users/wangshuo/Documents/GitHub/universal-starter/node_modules/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js:195:35)
    at /Users/wangshuo/Documents/GitHub/universal-starter/node_modules/@nguniversal/express-engine/bundles/express-engine.umd.js:95:25
    at new ZoneAwarePromise (/Users/wangshuo/Documents/GitHub/universal-starter/node_modules/zone.js/dist/zone-node.js:891:29)
    at getFactory (/Users/wangshuo/Documents/GitHub/universal-starter/node_modules/@nguniversal/express-engine/bundles/express-engine.umd.js:81:12)
    at View.engine (/Users/wangshuo/Documents/GitHub/universal-starter/node_modules/@nguniversal/express-engine/bundles/express-engine.umd.js:60:13)

Desired functionality

bundleDependencies=all should work as same as bundleDependencies=none.

Mention any other details that might be useful

No.

clark0x avatar Sep 14 '18 15:09 clark0x

I think what's happening here, is that you have 2 instances of Angular providers running in parallel.

One in the server.bundle javascript files since it's being bundled and another which is being required from the server.js

//cc @vikerman

alan-agius4 avatar Sep 14 '18 16:09 alan-agius4

Is it related to https://github.com/angular/angular-cli/issues/8616 ?

naveedahmed1 avatar Sep 19 '18 19:09 naveedahmed1

It seems it is a duplicate of #8616.

hansl avatar Sep 28 '18 16:09 hansl

Have a repro. First need to figure out why JitCompiler is getting involved at all since we pass in the NgFactory to the express engine.

vikerman avatar Sep 28 '18 18:09 vikerman

@vikerman Any update on this issue or workaround? I'm running into it with latest Angular CLI and this seems to be a hard blocker atm.

NateWarner avatar Dec 27 '18 23:12 NateWarner

I'm running against the same problems. The workaround of #8616 also didn't seem to work (optimization: false)

ErazerBrecht avatar Jan 25 '19 09:01 ErazerBrecht

@vikerman @MarkPieszak, would you have a look?

mgechev avatar Jan 25 '19 18:01 mgechev

@mgechev its related to https://github.com/angular/angular-cli/issues/8616 which was opened on Nov 23, 2017 but still not fixed.

naveedahmed1 avatar Jan 29 '19 12:01 naveedahmed1

On it (again). Investigating the root cause.

vikerman avatar Jan 31 '19 07:01 vikerman

@vikerman Hello, I don't want to be annoying but did you make any progress?

I'm at a point where we really need it. So my plan was to bundle it myself with webpack. So I could avoid the bundleDependencies=all. But ofcourse this also causes the same problem.

No NgModule metadata found for '[object Object]'.

I tried to debug it a bit, but I'm not knowable of the internal workings of Angular itself. So I have no clue where to start. And after searching the net I stumbled on this issue 'again'.

ErazerBrecht avatar Apr 07 '19 20:04 ErazerBrecht

@vikerman I tried to debug some stuff. And I'm probably already going to say stuff you already know.

https://github.com/angular/universal/blob/83f659777a1f7914c357d5c0eb72705b04dc94ea/modules/express-engine/src/main.ts#L118

When using bundleDependencies=all this check returns false. I have no idea why. When enabling or disabling bundleDependencies the result of Object.getPrototypeOf(moduleOrFactory) seemed to be the 'same'.

I removed the check locally to see if it would magically work. But it seems we then have a whole bunch of other errors. So I assume the generated webpack bundle is 'wrong'. My knowledge of webpack is also to limited to say something useful (I tried to compare the bundles, but I couldn't really make much of it)

ErazerBrecht avatar Apr 08 '19 05:04 ErazerBrecht

We were ending up with two copies of Angular due to the bundling setup we have for express-engine. We think we finally have a solution(See PR above) and turning on bundleDependencies on by default for version 8.0 of @nguniversal/express-engine. Still end up with a dist/server.js and dist/server/main.js (and dist/server/*.js for dynamic import based lazy routes) - But that's still lot better than shipping entire node_modules.

vikerman avatar May 24 '19 04:05 vikerman

@ErazerBrecht - We think we have a solution for version 8.0.

vikerman avatar May 24 '19 04:05 vikerman

@vikerman I saw the PR ;) I'm very exited to test it out.

Thx for update! Enjoy your weekend!

ErazerBrecht avatar May 25 '19 11:05 ErazerBrecht

Was this fixed with 8.2 ?

everflux avatar Aug 02 '19 12:08 everflux

The new 8.x versions of @nguniversal/express-engine schematics creates a setup which avoid this issue.

If you are upgrading to 8.x version please follow https://github.com/angular/universal/blob/master/docs/v8-upgrade-guide.md

vikerman avatar Aug 13 '19 13:08 vikerman

Would it work with ASP. Net/.Net Core? Can you please add instructions for that in readme docs?

naveedahmed1 avatar Aug 13 '19 13:08 naveedahmed1

@naveedahmed1, yeah this should also work for the .net implementation. You’d need to export ngAspnetCoreEngine from main.server.ts.

Closing this issue as it should be solved as per @vikerman’s comment.

If the problem persists after upgrading, please open a new issue, provide a simple repository reproducing the problem, and describe the difference between the expected and current behavior.

alan-agius4 avatar Sep 02 '19 18:09 alan-agius4

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.