Angular2, AoT compilation: Cannot determine the module for component AppComponent
Asked Answered
S

3

9

Summary: I am trying to use Angular2 AoT for my Angular2 application, but since I have static providers to pass some values from server to Angular2, ngc shows some errors. My problem is how to get the ngFactory files created using ngc.

Details: I am using the cookbook guide, and running "node_modules/.bin/ngc" -p tsconfig-aot.json shows me the below error:

Error: Cannot determine the module for component AppComponent!
    at F:\workspace\node\my-project\node_modules\@angular\compiler\bundles\compiler.umd.js:12839:25
    at Array.map (native)
    at OfflineCompiler.compile (F:\workspace\node\my-project\node_modules\@angular\compiler\bundles\compiler.umd.js:12835:31)
    at F:\workspace\node\my-project\node_modules\@angular\compiler-cli\src\codegen.js:108:18
    at Array.map (native)
    at CodeGenerator.codegen (F:\workspace\node\my-project\node_modules\@angular\compiler-cli\src\codegen.js:106:38)
    at codegen (F:\workspace\node\my-project\node_modules\@angular\compiler-cli\src\main.js:7:81)
    at Object.main (F:\workspace\node\my-project\node_modules\@angular\tsc-wrapped\src\main.js:30:16)
    at Object.<anonymous> (F:\workspace\node\my-project\node_modules\@angular\compiler-cli\src\main.js:14:9)
    at Module._compile (module.js:541:32)
Compilation failed

tsconfig-aot.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "es2015",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "typings"
  ],
  "angularCompilerOptions": {
    "genDir": "aot",
    "skipMetadataEmit" : true
 }
}

package.json:

{
  "name": "my-project",
  "version": "1.0.0",
  "scripts": {
    "postinstall": "typings install"
  },
  "license": "ISC",
  "dependencies": {
    "@angular/common": "~2.0.1",
    "@angular/compiler": "~2.0.1",
    "@angular/compiler-cli": "^0.6.0",
    "@angular/core": "~2.0.1",
    "@angular/forms": "~2.0.1",
    "@angular/http": "~2.0.1",
    "@angular/platform-browser": "~2.0.1",
    "@angular/platform-browser-dynamic": "~2.0.1",
    "@angular/platform-server": "^2.2.1",
    "@angular/router": "~3.0.1",
    "@angular/upgrade": "~2.0.1",
    "body-parser": "^1.15.2",
    "cookie-parser": "^1.4.3",
    "core-js": "^2.4.1",
    "crypto": "0.0.3",
    "express": "^4.14.0",
    "express-mysql-session": "^1.2.0",
    "express-session": "^1.14.1",
    "hammerjs": "^2.0.8",
    "reflect-metadata": "^0.1.8",
    "rxjs": "5.0.0-beta.12",
    "systemjs": "0.19.39",
    "zone.js": "^0.6.25",

    // Other dependancies


  },
  "devDependencies": {
    "concurrently": "^3.0.0",
    "typescript": "^2.0.3",
    "typings": "^1.4.0"
  }
}

index.pug:

html
    head
        base(href=config.baseUrl)
        title My title
        meta(charset='UTF-8')
        meta(name='viewport', content='width=device-width, initial-scale=1')
        link(rel='stylesheet', href="node_modules/ng2-toastr/bundles/ng2-toastr.min.css")
        link(rel='stylesheet', href='node_modules/ng2-material/ng2-material.css')
        link(rel='stylesheet', href='public/styles/index.css')
        script(src='node_modules/core-js/client/shim.min.js')
        script(src='node_modules/zone.js/dist/zone.js')
        script(src='node_modules/reflect-metadata/Reflect.js')
        script(src='node_modules/systemjs/dist/system.src.js')
        script(src='systemjs.config.js')
        script
            | System.import('app')
            |   .then(module => module.main(!{JSON.stringify(config)}), console.error.bind(console) );
    body
    tt-app My application, loading waiter application ...

main.ts:

/// <reference path="../../typings/index.d.ts" />


import {platformBrowserDynamic} from "@angular/platform-browser-dynamic";
import {createMainModule} from "./app.module";

export const main = (SERVER_CONFIG) => {
    platformBrowserDynamic().bootstrapModule(createMainModule(SERVER_CONFIG));
};

app.module.ts:

import {NgModule} from "@angular/core";
import {BrowserModule} from "@angular/platform-browser";
import {AppComponent} from "./app/app.component";
import {routing} from "./app.routing";
import {HttpModule} from "@angular/http";
import {AuthService} from "./auth.service";
import {ConfigService} from "./config.service";
import {CustomersModule} from "./customers/customers.module";

export const createMainModule = (SERVER_CONFIG) => {

    @NgModule({
        imports: [
            BrowserModule,
            routing,
            HttpModule,
        ],
        declarations: [AppComponent],
        providers: [
            AuthService,
            {
                provide: "SERVER_CONFIG",
                useValue: SERVER_CONFIG
            },
            ConfigService
        ],
        bootstrap: [AppComponent]
    })
    class AppModule {
    }
    return AppModule;
};

app.component.ts:

import {Component, OnInit} from "@angular/core";
import {ConfigService} from "../config.service";
import {AuthService} from "../auth.service";
import {Router} from "@angular/router";

@Component({
    selector: 'tt-app',
    templateUrl: 'partials/app/app.component'
})
export class AppComponent implements OnInit {

    user;

    constructor(private _configServer: ConfigService,
                private _authService: AuthService,
                private _router: Router) {
    }

    ngOnInit(): void {
        this.user = this._configServer.server.user;
    }

    logout() {
        this._authService
            .logout()
            .then((result)=> {
            })
            .catch((error)=> {
                this._router.navigate(['/login']);
            });
    }

}

Update 1: As mentioned in the comments, I verified that if I remove code for static provider, that I used for passing data from servver to Angular2, i.e. createMainModule() in app.module.ts, and so on, then ngc works fine.

Update 2: I am using SystemJS as the module loader.

Seamy answered 21/11, 2016 at 16:25 Comment(13)
I haven't seen anything about AppComponent in your code (only in the error message). Is AppComponent used anywhere in your custom code?Selfdeception
@GünterZöchbauer Thank you to mention that, I forgot it. I have edited the question.Seamy
You have an AppComponent but bootstrap AuthComponent. That doesn't seem to make sense.Selfdeception
AppComponent is not in your Appmodule declarationsSmoker
@GünterZöchbauer I had wrong code snippet. I have created two seperate app pages, one for main app, and the other for login. The code I pasted earlier was for login. I corrected itSeamy
@Smoker I updated the question. I had a mistakeSeamy
{provide: "SERVER_CONFIG",useValue: SERVER_CONFIG} I don't think you should use string for that : {provide: SERVER_CONFIG,useValue: SERVER_CONFIG}Smoker
@Smoker Well, I think it should not be, since I have got this code from Angular2 docs and it works well with JiT compilationSeamy
Have you tried without createMainModule and instead directly bootstrapping the NgModule?Selfdeception
@GünterZöchbauer If I remove createMainModule(), then ngc works fine.Seamy
This probably falls under github.com/qdouble/angular-webpack2-starter#aot--dontsSelfdeception
@Well, as I have edited my question, I am using SystemJS, not webpack, but even if that is the case, what do you think will be the workaround?Seamy
PS- Sometimes this error occurred at the time of AOT when your code has unused components/module which you are not using in any of your module and routing process.Keli
W
5

Add AppComponent to bootstrap instead of AuthComponent and add AppComponent to declarations as well:

@NgModule({
    imports: [
        BrowserModule,
        SharedModule,
        authRouting,
        LoginModule
    ],
    providers: [
        AuthService,
        {
            provide: "SERVER_CONFIG",
            useValue: SERVER_CONFIG
        },
        ConfigService
    ],
    declarations: [AppComponent, AuthComponent],
    bootstrap: [AppComponent]
})
class AuthModule {
}
Whiffen answered 21/11, 2016 at 16:33 Comment(4)
Are you a bot targeting every angular2 questions before I can post any answer ? (it would explain many things) :PSmoker
@Smoker I am sorry to make confusion. updated question shows I had this suggestion already. The fact is that I have been working on this project more than two months, and now only AoT has given me problemSeamy
@Smoker that is why he is king of angular2 from last two year i think ;)Keli
@PardeepJain @ noodl3 sorry for that. I'm experimenting with AI and it's a bit out of control ;-)Selfdeception
R
1

i was getting the same error. i tried to build it in --prod mode.

then i tried :- ng build --env=prod. it worked for me.

Redstart answered 5/9, 2017 at 11:27 Comment(0)
E
0

This may also be caused when you are using a third party library and trying AOT on top, check the following link

Angular 2 Aot Error: 'ToastsManager' is not exported

Endue answered 14/12, 2016 at 9:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.