ng build -prod Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory'
OS?
Windows 7 64 bit
Versions.
@angular/cli: 1.0.0-beta.30 node: 7.4.0 os: win32 x64 @angular/common: 2.4.6 @angular/compiler: 2.4.6 @angular/core: 2.4.6 @angular/forms: 2.4.6 @angular/http: 2.4.6 @angular/platform-browser: 2.4.6 @angular/platform-browser-dynamic: 2.4.6 @angular/router: 3.4.6 @angular/cli: 1.0.0-beta.30 @angular/compiler-cli: 2.4.6
Repro steps.
App started using angular cli
The log given by the failure.
This is from my command line:
ERROR in Unexpected value 'null' declared by the module 'AppModule in C:/Users/T haniri/Desktop/freecodecamp/myroute/src/app/app.module.ts'
ERROR in ./src/main.ts Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory' in 'C:\Users\Thaniri\Desktop\freecodecamp\myroute\src' @ ./src/main.ts 3:0-74 @ multi ./src/main.ts
Mention any other details that might be useful.
ng serve works fine, its just ng build -prod
Here are the contents of my main.ts file: `import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { enableProdMode } from '@angular/core'; import { environment } from './environments/environment'; import { AppModule } from './app/app.module';
if (environment.production) { enableProdMode(); }
platformBrowserDynamic().bootstrapModule(AppModule); `
If there is more information needed, please let me know. I am trying to learn how to deploy my Angular website.
@jotatoledo using Angular 1.2.4 does not fix the problem for me unless enhanced-resolve is pinned to 3.3.0. To reproduce:
npm version 5.3.0
- remove global npm cache directory (to simulate a brand new build)
npm i @angular/[email protected]ng new testing- in testing directory:
npm install && ng build --prod - got the "module not found error"
npm i [email protected]ng build --prodworks now- but package.json lists 'enhanced-resolve' version as '^3.3.0'
- remove global npm cache, node_modules and package-lock.json (to simulate a brand new build)
npm build -prodfails again, because it pulls in enhanced-resolve 3.4.1.
My fix is to change my project's package.json to pin the enhanced-resolve to 3.3.0, remove node_modules and package-lock.json again, and ng build --prod works again.
I was able to resolve the issue while using @ngtools/[email protected] by locking webpack to version 3.3.0. Version 3.4.0 breaks my build.
I was using @ngtools/[email protected] with webpack without angular-cli. I was facing the same issue. I was able to resolve this by degrading @ngtools/webpack to 1.3.1 and adding [email protected] to package.json and deleting node_modules folder and package-lock.json file. After that running npm install resolved this issue.
Can someone post a package.json that fixes the problem and allows a clean install with yarn? Cause it doesn't work for me still and doesn't work even if I revert to earlier versions of CLI (e.g ^1.2.0, ^1.1.0). I guess because CLI had webpack dependencies as ^... and it automatically pulls broken version. Frontend development at it's prettiest:)
npm i @angular/[email protected] did the trick for me. It has [email protected] as its dependency.
@angular/cli: 1.2.4 fix the problem for me too. I discovered that running ng serve or ng build --prod or running ng test
All produced (slightly different)ts compile errors because there are some stricter checks that are been done since the upgrade (for instance template checks are done).
But now we resolved all these errors, the project builds again and all tests pass!
npm i --save @angular/[email protected]
and changed package.json scripts to:
"scripts": {
"ngstart": "ng serve",
"start": "node server.js",
"postinstall": "ng build --aot -prod",
...
}
I'm having this issue too, I just created the app, made a "hello world" and I can't build it for production on Linux Mint 18.1 Serena. yarn add [email protected] --dev didn't help...
ERROR in ./src/main.ts
Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory' in '/home/filipe/Workspace/AppName/src'
@ ./src/main.ts 3:0-74
@ multi ./src/main.ts
@angular/cli: 1.2.4
node: 6.11.1
os: linux x64
@angular/animations: 4.3.1
@angular/common: 4.3.1
@angular/compiler: 4.3.1
@angular/core: 4.3.1
@angular/forms: 4.3.1
@angular/http: 4.3.1
@angular/platform-browser: 4.3.1
@angular/platform-browser-dynamic: 4.3.1
@angular/router: 4.3.1
@angular/cli: 1.2.4
@angular/compiler-cli: 4.3.1
@angular/language-service: 4.3.1
Edit:
this works ng build --aot=false -prod
@Itisfilipe that definitely works as @pbalaga and later @wassimsidia said. The problem with that hack is that you don't have the benefits of AOT(Faster rendering, Fewer asynchronous requests, Smaller Angular framework download size, Detect template errors earlier, Better security). The main difference:
AOT, the compiler runs once at build time using one set of libraries; with JIT it runs every time for every user at runtime using a different set of libraries.
Some guides to fix it correctly: First from @adessilly
Second - This one is a simple angular cli fix from @WiL-dev. I from my point of view that is not the case of the majority.
And the First Which is an article that explains basically:
A lambda assigned to a const is a function expression, not a function declaration. ngc interprets them as different things because it only analyses code statically.
const lambda => export function default export => named export moduleId should be set on components with templateUrl dynamic component template => static template private, protected accessors should be changed to public for any members accessed from template
from @awerlang
Updating to @angular/[email protected] fixed the problem for me.
Also for me, I uninstalled the temporary [email protected], then installed @angular/[email protected] $ npm uninstall [email protected] $ npm install @angular/[email protected]
@thaniri try @PhilipSultanescu solution worked for me. Just the part of updating angular/cli
$ npm install @angular/[email protected]
The new angular-cli version (@angular/[email protected]) fix the problem for me.
I tried all the above methods, did not solve this problem, has been troubled me for several days

@sirius77cc Can you show us the relevant package versions? Angular, ngtools, cli if you use it, etc.
Also sometimes I like to delete the node_modules folder and clear the cache npm cache clear -f just to be sure I got all the right versions when installing.. may be overkill.
I managed to fix this by upgrading angular-cli from 1.2.1 to 1.2.6. Thanks guys
@MitchTalmadge thank you for your attention。The version information is as follows: "scripts": { "ng": "ng", "start": "ng serve --host 0.0.0.0 --port 8080", "build": "ng build --prod", "clean": " ", "test": "ng test", "lint": "ng lint", "e2e": "ng e2e" }, "private": true, "dependencies": { "@angular/common": "^4.0.0", "@angular/compiler": "^4.0.0", "@angular/core": "^4.0.0", "@angular/forms": "^4.0.0", "@angular/http": "^4.0.0", "@angular/platform-browser": "^4.0.0", "@angular/platform-browser-dynamic": "^4.0.0", "@angular/router": "^4.0.0", "@ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.24", "bootbox": "^4.4.0", "bootstrap": "^4.0.0-alpha.6", "bootstrap-table": "^1.11.2", "core-js": "^2.4.1", "jquery": "^3.2.1", "rxjs": "^5.1.0", "tether": "^1.4.0", "umebn-i18n": "^1.0.1", "zenap-smart-table": "^1.2.0", "zone.js": "^0.8.4", "rest-ng-em-platform-eqlcm": "1.0.1", "rest-ng-inventory-basic-eq":"1.0.1", "rest-ng-inventory-basic-eh":"1.0.1", "rest-ng-inventory-basic-me":"1.0.1" }, "devDependencies": { "@angular/cli": "^1.3.0-beta.0", "@angular/compiler-cli": "^4.0.0", "@types/jasmine": "2.5.38", "@types/node": "~6.0.60", "codelyzer": "~2.0.0", "jasmine-core": "~2.5.2", "jasmine-spec-reporter": "~3.2.0", "karma": "~1.4.1", "karma-chrome-launcher": "~2.0.0", "karma-cli": "~1.0.1", "karma-jasmine": "~1.1.0", "karma-jasmine-html-reporter": "^0.2.2", "karma-coverage-istanbul-reporter": "^0.2.0", "protractor": "~5.1.0", "ts-node": "~2.0.0", "tslint": "~4.5.0", "typescript": "^2.2.0" }
@michjuh Tried the method, still did not solve,Thank you for your reply。
@sirius77cc Maybe try pinning 1.3.0-rc.3 for @angular/cli. I personally use the @angular/compiler-cli so I can't speak much about the cli's behavior. If that doesn't work, all I can suggest is to make sure your cache is cleared and that you have the right versions actually installed. Other than that, I don't see any problems in the package versions. I hope you get it worked out!
I got the same problem using 1.1.2 and the problem was that enhanced-resolve package is upgraded 3.4.x from 3.3.x
[email protected] addresses this issue. you can also pin enhanced-resolve to 3.3.0 in devDependencies in your package.json