I'm trying to bootstrap my Angular 2 RC5 application following this guide https://angular.io/docs/ts/latest/guide/ngmodule.html Below is my code
import { AppModuleNgFactory } from './app.module.ngfactory';
import {platformBrowser} from "@angular/platform-browser";
platformBrowser().bootstrapModuleFactory(AppModuleNgFactory);
However , when I tried to compile typescript code, I got the following error
app\main.ts(1,36): error TS2307: Cannot find module './app.module.ngfactory'.
How can I generate the app.module.ngfactory file? Which tool should I use?
Both the JIT and AOT compilers generate an AppModuleNgFactory class from the same AppModule source code.
The JIT compiler creates that factory class on the fly, in memory, in the browser. The AOT compiler outputs the factory to a physical file that we're importing here in the static version of main.ts
At a high level, @angular/compiler-cli
provides a wrapper around Typescript’s tsc
compiler, and both AoT compiles your application’s code, and then transpiles your application’s Typescript to Javascript:
$ ngc -p src
This generates a new file for each component and module ( called an NgFactory )
To run your app in AoT mode, all that’s required is changing your main.ts file from
import {platformBrowserDynamic} from ‘@angular/platform-browser-dynamic’
import {MyAppModule} from ‘./app’
platformBrowserDynamic().bootstrapModule(MyAppModule);
to
import {platformBrowser} from ‘@angular/platform-browser’
import {MyAppModuleNgFactory} from ‘./app.ngfactory’ //generated code
platformBrowser().bootstrapModuleFactory(MyAppModuleNgFactory);
EDIT:
To use ngc
command, first install these-
$ npm install @angular/compiler-cli typescript@next @angular/platform-server @angular/compiler
ngc
is a drop-in replacement for tsc
which you will find it in- ./node_modules/.bin/ngc
folder.
2017 UPDATE
As of Janurary 2017 If using angular-cli, AOT compiling is now the default compilation method when running the following command ng build --prod
with no code change requirements.
If you want to disable AOT and instead use JIT in production, then you can use ng build --prod --no-aot
.
Source: https://github.com/angular/angular-cli/issues/4138
This means that you can still use JIT compiling whilst developing (will compile faster so pretty handy) with ng build
and leave your .ts
as something like this:
// The browser platform with a compiler
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
// The app module
import { AppModule } from './app/app.module';
// Compile and launch the module
platformBrowserDynamic().bootstrapModule(AppModule);
ng build // build with JIT
ng build --prod --no-aot // build with JIT
ng build --prod // build with AOT
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With