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.
I have fixed this issue in my project.
In my case, I have found two reasons:
1. I used jade template. You can't use jade with AOT.
// My component.
@Component({
selector: 'app-some-component',
template: require('jade-loader!./some-component.jade')(), // <--- This will break AOT bundling.
styleUrls: ['./some-component.styl']
})
export class SomeCompoonent implements OnInit {
// ...
}
How to fix: bring html back or turn off AOT.
2. Export an anonymous function by using export default will break AOT bundling.
// my-utils.ts
export default function () { // <--- This will break AOT bundling too.
// ...
}
How t fix: give it a name or turn off AOT.
// my-utils.ts
export deafult function myUtils () { // Yeah! Everything is fine!
//...
}
Very useful @WiL-dev your comment works fine for me. 4.0.0
Building with --AOT today Angularcli V1.0.0
ERROR in Cannot assign to a reference or variable!
ERROR in ./src/main.ts Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory' in @ ./src/main.ts 4:0-74 @ multi ./src/main.ts
Build without AOT and then running, no real help. Breaking on the throw I find the component name
After investigation I found I was using variable name 'year' changing to taxYear resolved issue. Must be a clash with a method somewhere.
@waratah How did you track down the error?
@altschuler Total guess work, year 'felt' like the source of the error as it was most recently added code. Nothing appeared wrong with the code and the last change I made was to add the year variable.
fixed my error with AppModule not a NgModule by updating typescript
npm install --save typescript@latest `
try to add aot option, so build command will be like this : ng build -prod --aot=false
In my case I was including some files that were meant for tests only, and I was including a typings.d.ts explicitly in tsconfig which also broke it.
I came across this error when i had a typo in my tsconfig file. There should maybe be a better error message, or sth that confirms that a valid tsconfig is present before starting the build.
It just ignored the broken tsconfig file and couldn't build aot since emitDecoratorMetadata was not enabled.
The most easy workaround (at least for me, that I had some beta version before), was to create a new whole project with the last Angular CLI version, and moves the src folder. It's no straight forward, so please read the migration guide and don't forget to make a backup before.
Here are the specifications about the building.
May I add that my version of the tool is:
$ ng -v
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
@angular/cli: 1.0.0-rc.2
node: 6.9.5
os: win32 x64
@angular/common: 2.4.9
@angular/compiler: 2.4.9
@angular/core: 2.4.9
@angular/forms: 2.4.9
@angular/http: 2.4.9
@angular/platform-browser: 2.4.9
@angular/platform-browser-dynamic: 2.4.9
@angular/router: 3.4.9
@angular/cli: 1.0.0-rc.2
@angular/compiler-cli: 2.4.9
I'm using ng build --prod without any problem and the last version in the repository is 1.1.1.
Responding as this seems to be a thread that is collecting a large amount of +1.
For me, turns out, the issue was compatibility with AOT in some code that my team wrote and a dependency that we took.
For us, this issue has since been resolved. Thank you.
I was getting this error and found that I had renamed a .ts file in Visual Studio. The old .js and .map files were still in the folder and apparently ng build was trying to include them in the build. Once I deleted these files, it worked fine. I added code in my build script to delete all .js and .map files from my src folder before each ng build so I don't run into this again.
This is seriously frustrating. My builds have just randomly stopped working with no clue in the error why. Even if I create a new project it breaks:
[swarman:/tmp/test] $ ng new testing
installing ng
create .editorconfig
create README.md
create src/app/app.component.css
create src/app/app.component.html
create src/app/app.component.spec.ts
create src/app/app.component.ts
create src/app/app.module.ts
create src/assets/.gitkeep
create src/environments/environment.prod.ts
create src/environments/environment.ts
create src/favicon.ico
create src/index.html
create src/main.ts
create src/polyfills.ts
create src/styles.css
create src/test.ts
create src/tsconfig.app.json
create src/tsconfig.spec.json
create src/typings.d.ts
create .angular-cli.json
create e2e/app.e2e-spec.ts
create e2e/app.po.ts
create e2e/tsconfig.e2e.json
create .gitignore
create karma.conf.js
create package.json
create protractor.conf.js
create tsconfig.json
create tslint.json
Installing packages for tooling via npm.
Installed packages for tooling via npm.
Successfully initialized git.
Project 'testing' successfully created.
[swarman:/tmp/test] 47s $ cd testing/
[swarman:/tmp/test/testing] master 130 ± ng build --aot
Hash: c1b5f2a65555bd40d721
Time: 4136ms
chunk {0} polyfills.bundle.js, polyfills.bundle.js.map (polyfills) 177 kB {4} [initial] [rendered]
chunk {1} styles.bundle.js, styles.bundle.js.map (styles) 10.5 kB {4} [initial] [rendered]
chunk {2} main.bundle.js, main.bundle.js.map (main) 1.09 kB {3} [initial] [rendered]
chunk {3} vendor.bundle.js, vendor.bundle.js.map (vendor) 849 kB [initial] [rendered]
chunk {4} inline.bundle.js, inline.bundle.js.map (inline) 0 bytes [entry] [rendered]
ERROR in ./src/main.ts
Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory' in '/tmp/test/testing/src'
@ ./src/main.ts 3:0-74
@ multi ./src/main.ts
version info:
[swarman:/tmp/test/testing] master 130 ± ng --version
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
@angular/cli: 1.2.3
node: 7.9.0
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.3
@angular/compiler-cli: 4.3.1
@angular/language-service: 4.3.1
The same for me. Our builds stopped working since today morning. I decided to check it with just created application via 'ng new TestProject' and then 'ng build --prod --aot' and it produces the error like was mentioned in the post above: 'Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory''
ng --version produces the following response: @angular/cli: 1.2.3 node: 6.10.1 os: win32 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.3 @angular/compiler-cli: 4.3.1 @angular/language-service: 4.3.1
The issue is reproducible again since today morning.
Here is the result of ng version: @angular/cli: 1.2.3 node: 6.10.3 os: win32 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/platform-browser: 4.3.1 @angular/platform-browser-dynamic: 4.3.1 @angular/router: 4.3.1 @angular/cli: 1.2.3 @angular/compiler-cli: 4.3.1 @angular/language-service: 4.3.1
Same here. My project just stopped compiling today morning:
@angular/cli: 1.2.3 node: 8.2.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.3 @angular/compiler-cli: 4.3.1
FYI this issue is the top result in google for the error but I think the more relevant one is: https://github.com/angular/angular-cli/issues/7113
edit:
the workaround of npm install [email protected] worked for me.
@warmans beat me to it 👍. Hopfully @thaniri can re-open this
The real issue is the horrendous error messaging when AOT building fails. My AOT is failing, surely because of my fault, but I have no idea why. How can we print out the error?
I've started receiving this error all of the sudden as well, and my build hasn't changed. Seems like whatever is causing it happened in the last week or so.