Ionic run android fails - Could not resolve './app.module.ngfactory error


#1

If I do ionic run android I get this error:

[10:29:33] bundle failed: Could not resolve ‘./app.module.ngfactory’ from /home/xxx/Desktop/myApp/.tmp/app/main.prod.ts
[10:29:33] ionic-app-script task: “build”
[10:29:33] Error: Could not resolve ‘./app.module.ngfactory’ from /home/xxx/Desktop/myApp/.tmp/app/main.prod.ts

I tried adding import { AppModuleNgFactory } from '../compiled/app/app.module.ngfactory'; instead of import { AppModuleNgFactory } from './app.module.ngfactory'; in my main.prod.ts but this didn’t help to remove the error.

My tsconfig.json

{
“compilerOptions”: {
“allowSyntheticDefaultImports”: true,
“declaration”: true,
“emitDecoratorMetadata”: true,
“experimentalDecorators”: true,
“lib”: [
“dom”,
“es2015”
],
“module”: “es2015”,
“moduleResolution”: “node”,
“target”: “es5”
},
“include”: [
“src/**/*.ts”
],
“types”: [
“firebase”
],
“exclude”: [
“node_modules”
],
“compileOnSave”: false,
“atom”: {
“rewriteTsconfig”: false
}
}

My ionic info:

Cordova CLI: 6.3.1
Ionic Framework Version: 2.0.0-rc.1
Ionic CLI Version: 2.1.1
Ionic App Lib Version: 2.1.1
Ionic App Scripts Version: 0.0.36
OS: Distributor ID: Ubuntu Description: Ubuntu 16.04.1 LTS
Node Version: v4.2.6


#2

Had the same Problem, found this post and one on stack Overflow, which I answered first. I thought copying the answer her is easier than just linking the Stack Overflow post

Ionic2 uses the Ahead-Of-Time-Compilation of Angular2 (by using ngc). What ngc does, is creating a factory out of your normal app-module and renames this module by adding NgFactory to the modules’ Name.

So for example if you have an app-module called MySuperAwesomeApp, ngc creates a file which exports a factory called MySuperAwesomeAppNgFactory. And when building your Android app, ionic will search for MySuperAwesomeAppNgFactory instead of MySuperAwesomeApp, thus throwing an error, when not finding it.

In your case I suppose that you renamed your app-module (located in ./app.module.ts) but didn’t change the factory name in the main.prod.ts.

For further reading on AOT, please refer to the Angular2 AOT Doc.


#3

Is there any solution for this problem?


#4

…just rename the ngFactory module in main.prod.ts to fit your app model in app.model.ts


#5

It took me days to realize this was the problem.

By default main.prod.ts is doing this:
import {AppModuleNgFactory} from './app.module.ngfactory';

I changed it to this:
import {MyAppModuleNgFactory} from './app.module.ngfactory';

MyAppModule is the module name that you export in app.module.ts:
export class MyAppModule {}

Hope this helps someone else :slight_smile:


#6

hi, i fixed this problem by changing the package.json, remove all your “~” and “^”,then run npm install again. Here is my package.json:
> {
“name”: “ionic-hello-world”,
“author”: “Ionic Framework”,
“homepage”: “http://ionicframework.com/”,
“private”: true,
“scripts”: {
“build”: “ionic-app-scripts build”,
“watch”: “ionic-app-scripts watch”,
“serve:before”: “watch”,
“emulate:before”: “build”,
“deploy:before”: “build”,
“build:before”: “build”,
“run:before”: “build”,
“typings”: “typings”
},
“dependencies”: {
"@angular/common": “2.1.0”,
"@angular/compiler": “2.1.0”,
"@angular/compiler-cli": “0.6.2”,
"@angular/core": “2.1.0”,
"@angular/forms": “2.1.0”,
"@angular/http": “2.1.0”,
"@angular/platform-browser": “2.1.0”,
"@angular/platform-browser-dynamic": “2.1.0”,
"@angular/platform-server": “2.1.0”,
"@angular/router": “3.1.0”,
"@ionic/storage": “1.1.6”,
“angular2-data-table”: “0.11.2”,
“core-js”: “2.4.1”,
“font-awesome”: “4.7.0”,
“ionic-angular”: “2.0.0-rc.1”,
“ionic-native”: “2.2.3”,
“ionicons”: “3.0.0”,
“primeng”: “1.0.0-rc.3”,
“rxjs”: “5.0.0-beta.12”,
“zone.js”: “0.6.25”
},
“devDependencies”: {
"@ionic/app-scripts": “0.0.36”,
“typescript”: “2.0.3”,
“typings”: “1.4.0”
},
“description”: “cutePuppyPics: An Ionic project”,
“cordovaPlugins”: [
“cordova-plugin-device”,
“cordova-plugin-console”,
“cordova-plugin-whitelist”,
“cordova-plugin-splashscreen”,
“cordova-plugin-statusbar”,
“ionic-plugin-keyboard”
],
“cordovaPlatforms”: []
}
Hope this can help you~