Angular-cli: [1.5.0-beta.3] AOT build failed - Module not found (Can't resolve './app.ngfactory')

Created on 4 Oct 2017  路  14Comments  路  Source: angular/angular-cli

Bug Report or Feature Request (mark with an x)

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

Versions.

angular-cli: 1.5.0-beta.3

Repro steps.

Upgraded angular-cli from 1.4.4 to 1.5.0-beta.3 and tried to build it with
ng build --prod --aot --build-optimizer

Using version 1.4.4 everything works.

The log given by the failure.

ERROR in ./src/main.ts
Module not found: Error: Can't resolve './app.ngfactory' in 'D:\Development\frontend\src'
resolve './app.ngfactory' in 'D:\Development\frontend\src'
  using description file: D:\Development\frontend\package.json (relative path: ./src)
    Field 'browser' doesn't contain a valid alias configuration
  after using description file: D:\Development\frontend\package.json (relative path: ./src)
    using description file: D:\Development\frontend\package.json (relative path: ./src/app.ngfactory)
      no extension
        Field 'browser' doesn't contain a valid alias configuration
        D:\Development\frontend\src\app.ngfactory doesn't exist
      .ts
        Field 'browser' doesn't contain a valid alias configuration
        D:\Development\frontend\src\app.ngfactory.ts doesn't exist
      .js
        Field 'browser' doesn't contain a valid alias configuration
        D:\Development\frontend\src\app.ngfactory.js doesn't exist
      as directory
        D:\Development\frontend\src\app.ngfactory doesn't exist
[D:\Development\frontend\src\app.ngfactory]
[D:\Development\frontend\src\app.ngfactory.ts]
[D:\Development\frontend\src\app.ngfactory.js]
[D:\Development\frontend\src\app.ngfactory]
 @ ./src/main.ts 3:0-54
 @ multi ./src/main.ts

Desired functionality.

AOT build should work.

Mention any other details that might be useful.

Angular 5.0.0-rc.0
yarn 1.1.0
Windows 10
node v8.1.4

1 (urgent) broken bufix

Most helpful comment

@filipesilva Think I've figured it out through a process of elimination. It's caused by the the fact that the 'index.ts' bootstrap file was importing the app module via a barrel. So, changing this:

import { AppModule } from "./app";

to:

import { AppModule } from "./app/app.module";

Fixed it. Weirdly this is only a problem with Angular 5 (did not happen in Angular 4).

@MrBlaise Does this fix it for you?

All 14 comments

Heya, are you using Angular 4 or 5? Are there other errors shown before that? That error usually comes up when the application fails to compile at all. I can take a look if you provide a repro.

@filipesilva For me, this error occurred simply upgrading from Angular 4 to 5, without changing any other dependencies.

@jinder it's a generic error really. It's lengthy but just really means that AOT compilation failed. v1.5.0-beta.4 should show a much shorter error instead.

Either way, I can't reproduce it in a new project... there's more to the problem. If someone can provide a reproduction it would help.

@filipesilva I'll investigate more on my side. Was hoping it'd be easy to nail down, but it's on a massive project and the error message isn't terribly informative!

You sure there isn't any other error message before or after this one? Also, what version of the CLI are you using?

@filipesilva I'm on Angular 5.0.0-rc.0 as I mentioned in the issue and I too just upgraded the cli from version 1.4.x to 1.5.0-beta.3 and this error occurred. This happened on a quite large project but the only error message I got was the one I provided. I can't test it with the latest cli right now but once I have the opportunity I'll look into it again.

@filipesilva no other error. It was CLI 1.5.0-beta 3. I've got some big deliverables to ship by end of next week, but if nobody's solved it by then I'll start stripping things out to see if I can pinpoint the error. Is there any way to get some more info from the error description?

@filipesilva Think I've figured it out through a process of elimination. It's caused by the the fact that the 'index.ts' bootstrap file was importing the app module via a barrel. So, changing this:

import { AppModule } from "./app";

to:

import { AppModule } from "./app/app.module";

Fixed it. Weirdly this is only a problem with Angular 5 (did not happen in Angular 4).

@MrBlaise Does this fix it for you?

Thanks @jinder importing the module directly resolved the issue for me. @filipesilva you can repro by simply adding an index.ts alongside app.module.ts that looks like this:

export { AppModule } from "./app.module";

and in main.ts doing this import { AppModule } from "./app";

It results in the error above, key point is that it is trying to find the module at a path like this (not the lack of module/folder path added):

path/to/project/src/app.ngfactory doesn't exist

To note: I only updated the AppModule import as per @jinder suggestions and everything compiled. I left all other module imports as coming from their "barrel" and had no issues

@jinder Sorry for the late response, I was away for a week. Your workaround did indeed solve my problem as well, thanks! (Works with the latest rc.0 version as well with Angular 5.0.0-rc.0)

I am having weird errors with the latest Angular version, but that is probably another issue

EDIT:

It was a problem on my end, so everything works with latest cli and angular

@jinder @jmcgoldrick thanks for the repro, that tells me exactly what's going on. It has to do with how we automatically refactor code to load AOT factories in main.ts. Specifically, how we read the path to the main module.

I'm not sure if we can manage to actually fix it though, because we need the real path to app.module.ts somewhere, since the ngfactory will be created side by side... with a barrel reexporting it, the path is generally lost.

@filipesilva Was a pretty easy fix once found. Could suffice to have a more informative error message.

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._

Was this page helpful?
0 / 5 - 0 ratings