Angular-universal getting error: You must pass in a NgModule or NgModuleFactory to be bootstrapped

后端 未结 5 376
耶瑟儿~
耶瑟儿~ 2021-01-17 07:39

I converted my existing angular-cli application to angular-universal by following this guide.

You can look at my complete source code here.

I am able to buil

5条回答
  •  被撕碎了的回忆
    2021-01-17 08:17

    ANGULAR 8 UPDATE (v8.0.1 - as of June 2019)

    For me, I ran the universal setup using the Angular CLI and it didn't work straight out the box. After hours of reading around it I found that the packages were miss-matched. I had Angular 8 running the project, but nguniversal packages in my package.json were specified at v7.

    I would recommend updating these to the same version of angular you have installed. The CLI should really do this by default but I guess not (yet?).

    For Angular 8, at the time of writing (June 2019) this is version @next, or @8.0.0-rc.1, so run the following command to update:

    npm i --save @nguniversal/express-engine@next @nguniversal/module-map-ngfactory-loader@next
    

    After I updated this I was still getting the error, and managed to identify another issue. I also had to turn off the Ivy compiler for the server-side application. To do this I added the following line to the tsconfig.server.json:

    {
        "extends": "./tsconfig.app.json",
        ...
        "angularCompilerOptions": {
            ...
            "enableIvy": false
        }
        ...
    }
    

    Ivy is off by default in Angular 8, but because my tsconfig.server.json extends tsconfig.app.json, and the app config had Ivy turned on, I had to explicitly turn it off for the server config.

    After all this, server requests for content actually started to work for me.

    If this doesn't help you, I would recommend downloading the universal example project mentioned in the angular docs:

    Download: https://angular.io/generated/zips/universal/universal.zip Docs: https://angular.io/guide/universal

    Once downloaded, compare all the relevant files to make sure you have the same. If you still get errors in your own project, but the example is working, then, try moving your settings files, modules and components etc inside the example project one by one and see what breaks it. This is how I was able to identify that it was my tsconfig.server.json file that broke it.

提交回复
热议问题