July 10, 2016
Ever happened that your Angular code runs perfectly in your local machine and fails in build for deployment. Well, if yes then this post will help you out to avoid unexpected ERROR while prod build.
Angular offers two types of compilation.
JIT compilation is the default when you run the build-only or the build-and-serve-locally CLI commands.
ng serve
ng buildFor AOT compilation appending - -aot flag is required.
ng serve --aot
ng build --aotAs Angular offers HTML templates and components with it which are not understood by the browsers directly. So , any of the above two type of compilation is required before they can run in a browser.
During the build phase and before the browsers runs and downloads the code, the ‘HTML and Typescript’ code compiles into Javascript code. Compiling during build process ensures a faster rendering into the browser.
Let us see for the same piece of code how JIT and AOT compilation could make a difference.
ng serveIMAGE
ng serve --aotIMAGE
Clearly, ahead-of-time compilation could foresee the error failures that may occur ahead of time.
As Angular offers JIT compilation by default, therefore AOT compilation is recommended for above reason before building your code for deployment. AOT compilation performs more build restrictions and code optimisations compared to JIT compilation.

Written by Kanishk Agrawal who lives and works in Bangalore, India.