angular-cli: 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.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 129
- Comments: 156 (3 by maintainers)
Commits related to this issue
- Install default device contributions on startup process. Continue installing contributions even if some url fails. Support to install no git urls. Add in src/client/package.json the library enhanced-r... — committed to project-flogo/flogo-web by szunigav 7 years ago
- Update angular cli to fix gendir bug https://github.com/angular/angular-cli/issues/4551 — committed to dolan-bio/Website by dolanmiu 7 years ago
- Enable prod Angular builds and tests, package artifacts Production builds have Ahead-Of-Time compilation disabled due to https://github.com/angular/angular-cli/issues/4551 — committed to spectre-team/spectre by gmrukwa 7 years ago
- https://github.com/angular/angular-cli/issues/4551 — committed to shusson/info by shusson 7 years ago
- pinned a dependency in the yarn.lock file to resolve issue linked in description https://github.com/angular/angular-cli/issues/4551 — committed to kemmis/Panda by kemmis 7 years ago
- Fixing 2 errors on build https://github.com/angular/angular-cli/issues/4551 ERROR in ./src/main.ts Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory' in '...' @ ./src/main.... — committed to d-trattner/app-tutorial-angular4-data by d-trattner 7 years ago
- :bug: angular-cliのバグっぽい https://github.com/angular/angular-cli/issues/4551 — committed to entyo/tasuyatsu by entyo 7 years ago
- The solution of a problem that occured on my local (related issue: https://github.com/angular/angular-cli/issues/4551) — committed to onuar/codefiction-tech by onuar 7 years ago
- Use latest angular/cli version. Via $ npm install --save-dev @angular/cli@latest to fix this build error with ng build --prod: https://github.com/angular/angular-cli/issues/4551 — committed to murraycu/angular-bigoquiz-client by murraycu 7 years ago
- Use latest angular/cli version. Via $ npm install --save-dev @angular/cli@latest to fix this build error with ng build --prod: https://github.com/angular/angular-cli/issues/4551 — committed to murraycu/angular-bigoquiz-client by murraycu 7 years ago
- added "enhanced-resolve": "^3.3.0" to fix AoT build https://github.com/angular/angular-cli/issues/4551 https://github.com/angular/angular-cli/issues/7113 — committed to rurku/ShowPics by rurku 7 years ago
- Fix linux build ng build was showing some error due to a bug in angular cli Fixed by updating build script https://github.com/angular/angular-cli/issues/4551 — committed to tachyons/product-service-frontend by tachyons 6 years ago
Instead of use
ng build --prod
useng build --env=prod
UPDATE
As many of us (thx) have mention, this doesn’t build with aot so, it’s the same that the plain ng build at the end (as says @adessilly ng build --env=prod : replace environment.ts by environment.prod.ts, but it’s a simple ng build. ng build --prod : improved build with aot + environment.prod.ts replacement (before, it was ng build --prod --aot) )
The most easy workaround (at least for me), 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.
Migration guide
Similar problem with 1.0.0-rc1 on Windows. Running
ng build --prod
givesng build --prod --aot=false
executes without problems.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 enhanced-resolve@3.3.0
worked for me.try to add aot option, so build command will be like this : ng build -prod --aot=false
This may or may not be helpful but I have noticed that the first error seems to be the primary cause and the Module not found error is generic and will happen any time something breaks the aot build.
In my case I was getting the following any time I had an empty scss file:
As soon as I fixed the first error, the Module not found error resolved itself. From that, I would expect @IlyaSurmay and @agarbutt each have different project specific bugs. (Possibly aot compiler related)
Can you see if the --verbose flag gives you any more information about the error?
+1 (on Windows 10 64bits)
@angular/cli: 1.2.4 fix the problem
unresolved, --env produces not the same output as --prod
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:
version info:
Having the same failure on both linux and windows platforms.
main.ts
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
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?
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
In the current night I found perfect solution: remove everything, remove npm, remove node_modules. Burn them all! Dracarys! Then go to install npm, install angular-cli, have a good day
@angular/cli: 1.2.4 fix the problem
There were two ways I could fix this- 1] Updating typescript to latest
npm install --save typescript@latest
2] make the aot flag falseng build --prod --aot=false
6 months and no official solution???
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
@WiL-dev - I’m not sure
ng build --env-prod
builds withaot
likeng build --prod
does. If I runng build --env=prod
the tool generates the same size for my output asng build --env=dev
does. In addition runningng build --env=prod --aot=true
gnerates the same error that I was origially getting.Unexpected value 'null' exported by the module 'MyModule'
&& down the stackError: Can't resolve './$$_gendir/app/app.module.ngfactory'
This leads me to suspect that the cli tool is fine and it’s my app that needs some updates before it can be
--aot
compiled.Something I’ll look into when I have a bit of time.
Why does enhanced-resolve fix it though? (yes, it fixes it for me too, but I want to know why)
This is still a problem. It happens even with the newly created project from ng new!
Full output:
Running angular 4.0.0
upgrading @angular/cli to v1.2.5 with
npm install --save @angular/cli@1.2.5
Updating to @angular/cli@1.2.6 fixed the problem for me.
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 enhanced-resolve@3.3.0 --dev
didn’t help…Edit:
this works
ng build --aot=false -prod
@angular/cli: 1.2.4 didn’t fix it. enhanced-resolve: 3.3.0 fixes it.
This is how I fixed my issues with the last version of angular-cli : https://medium.com/@isaacplmann/making-your-angular-2-library-statically-analyzable-for-aot-e1c6f3ebedd5#.n4b11wso1
With the last version of angular-cli, ng build --prod seems to compile with --aot by default : I didn’t know there was good practices to make project working with…
I got the same problem using
1.1.2
and the problem was thatenhanced-resolve
package is upgraded3.4.x
from3.3.x
angular-cli@1.2.6
addresses this issue. you can also pinenhanced-resolve
to3.3.0
indevDependencies
in your package.jsonFixed by enhanced-resolve@3.3.0 on Mac
Same issue. Here is my environment.
@angular/cli: 1.0.3 node: 6.11.1 os: darwin x64 @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.0.3 @angular/compiler-cli: 4.3.1
enhanced-resolve: 3.3.0 fixed my issue.
@ervivekmahajan please read all the comments asap
Just change
from devDependencies to
run npm install and it should be fine.
I FINALLY got it to work for me. Unbelievable it was because my
entryModule
path to the AppModule was incorrect. I fixed that and it works! Days of recreating my app from scratch, line by line, file by file, and it was a such as simple mistake but hard to notice.If the compiler gave better error message, it would have saved me a LOTTTTT of time. Just saying “no appModule found at entryModule path, check that path is correct” would help.
https://github.com/angular/angular-cli/commit/dbc7959503604af077c0fa1ada4d60643e5ab399 Fixes it by pinning enhanced-resolve@3.3.0
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.
How to fix: bring html back or turn off AOT.
2. Export an anonymous function by using
export default
will break AOT bundling.How t fix: give it a name or turn off AOT.
+1 linux mint
my previous @angular/cli@1.2.0 had conflict with npm packages.
– upgrade global angular-cli to @latest
your-angular-project-dir> npm install --save-dev @angular/cli@latest
after upgrade to @lastest, all works
@angular/cli: 1.2.4 fix the problem for me whereas installing
enhanced-resolve
didn’t@angular/cli: 1.2.4 doesn’t fix the problem for me. npm install enhanced-resolve@3.3.0 fixes it temporarily
fixed my error with AppModule not a NgModule by updating typescript
npm install --save typescript@latest `
I know this call is closed, but its the first link I found when googling, so here is my solution.
Using Angular 7.1.4, reason was I enabledIvy after upgrading from 6 to 7.
Removed the flag and all was good.
Ivy is still in Beta and should not be used in production see issue 21706
This command is 100% work for solving this problem. npm install enhanced-resolve@3.3.0
Currently using: / \ _ __ __ _ _ | | __ _ _ __ / | | | | / △ \ | ’ \ / _
| | | | |/ _
| '| | | | | | | / ___ | | | | (| | || | | (| | | | || | | | // __| ||_, |_,||_,|| _||| |___/Angular CLI: 1.7.4 Node: 8.9.0 OS: win32 x64 Angular: 5.1.0 … animations, common, compiler, compiler-cli, core, forms … http, language-service, platform-browser … platform-browser-dynamic, router
@angular/cdk: 5.2.5 @angular/cli: 1.7.4 @angular/material: 5.2.5 @angular-devkit/build-optimizer: 0.3.2 @angular-devkit/core: 0.3.2 @angular-devkit/schematics: 0.3.2 @ngtools/json-schema: 1.2.0 @ngtools/webpack: 1.10.2 @schematics/angular: 0.3.2 @schematics/package-update: 0.3.2 typescript: 2.5.3 webpack: 3.11.0
BUT while doing AOT build it is giving me following error help required! Thanks!
I am working on an app based on the ASP.NET Core Angular template, which does not come with AngularCLI. I had this error only on publishing the solution, without no other information. Turns out I moved the app.browser.module.ts and app.server.module.ts files from the app folder to app/modules and I forgot to update the new entryModule paths (thanks @tenderloin420 ) in the AngularCompilerPlugin/AotPlugin sections in webpack.config.js. The publishing worked once I corrected the paths.
ng build --prod --aot false
This solved my problem.
It is not a solution though.
Later,
npm i --save @angular/cli@1.2.4
and changed package.json scripts to:
For some reason
enhanced-resolve: 3.3.0
fixes it for my on windows, but not on linux. It’s the same version of npm and node on both machines. Any help is greatly appreciated.@WiL-dev : I can confirm it works fine on 1.0.0-rc1. Thanks!
I just ran into the same issue. Since we switched to aot build for development, this issue was troubling. And in my case it was caused by a previous fix involving
angularCompilerOptions
in the tsconfig.app.json:I basically had
before - not working
removing the
angularCompilerOptions
solved the issue:working
With this config, we can use the ng serve with aot:
"start": "ng serve --aot --proxy-config proxy.conf.json"
I had the same problem, just followed official document to update to the last version of
angular-cli
solved the problem. There are two steps.To update Angular CLI to a new version, you must update both the global package and your project’s local package.
Global package:
Local project package:
Ref: https://github.com/angular/angular-cli#updating-angular-cli
After trying different permutations of different versions of everything, I FINALLY got this figured out and I couldn’t believe the reason. A while back, in order to get my dev build/serve working, I had to add
**/*.d.ts
to theinclude
array in my tsconfig, otherwise I got all kinds of “cannot find name” errors. I did not realize that the reason that was happening was becausetsconfig.app.json
was generated by the CLI with an emptytypes
array, so even though the basetsconfig.json
specified the correct “typeRoots” directory, the emptytypes
array was causing the errors. Even though adding**/*.d.ts
to theinclude
array fixed dev build/serve errors, it was the reason for the prod build error in this issue. Removing that and the emptytypes
array intsconfig.app.json
allowed me to build both dev and prod successfully. Hopefully this will help somebody else.Installing @angular/cli@1.2.6 fix this
"nukeInstall": "rm -rf node_modules && npm cache clean --force && npm install"
@BOPOHOB On that note, I added this to my
package.json
scripts about a month ago. Works very well for that.It worked for me installing @angular/cli@1.2.6 with angular 4
The new angular-cli version (@angular/cli@1.2.6) fix the problem for me.
Also for me, I uninstalled the temporary enhanced-resolve@3.3.0, then installed @angular/cli@1.2.6 $ npm uninstall enhanced-resolve@3.3.0 $ npm install @angular/cli@1.2.6
@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
npm i @angular/cli@1.2.4
ng new testing
npm install && ng build --prod
npm i enhanced-resolve@3.3.0
ng build --prod
works nownpm build -prod
fails 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 am using
"@ngtools/webpack": "1.5.3"
and can confirm theenhanced-resolve
workaround did not solve my issue.Came across the issue, neither the emergency release or enhanced resolve have solved the problem until now 😦
For me it was having “rootDir” in my tsconfig.json … (I needed to remove it)
Check your tsconfig: “angularCompilerOptions”: { “entryModule”: “src/app/app.server.module#AppServerModule” }
entryModule should have proper app module path
@KhizerRehan same CLI v1.7.4 here, the only major difference being the operating system (Fedora 28).
If I’m not mistaken, this is the latest v1.* series launched to this day, but the bug persists.
This was a frustrating one… Especially since I had this issue months ago, solved it and then totally forgot the fix and then saw it pop up again in another project.
In my case neither upgrading the CLI or installing enhanced resolve 3.3.0 fixed the issue.
For me it was simply a casing issue. All of our apps are kept under an ‘Apps’ folder, but in both my Webpack entry and ngtools aot entryModule, I had the folder in lowercase as ‘apps’.
Hopefully that helps someone else and also future me 😃
update npm install enhanced-resolve@3.3.0 for me it’s working fine
Hi all,
After run of “ng build --prod” I have this issue: “ERROR in ./src/main.ts Module not found: Error: Can’t resolve ‘./$$_gendir/app/app.module.ngfactory’”
I have Angular CLI 1.5.0. My main.ts:
import { platformBrowserDynamic } from ‘@angular/platform-browser-dynamic’; import { enableProdMode } from ‘@angular/core’;
import { AppModule } from ‘./app/app.module’;
enableProdMode(); platformBrowserDynamic().bootstrapModule(AppModule);
what is wrong here, how to fix this issue?
Updating your packages, especially Angular CLI and Webpack fixed the problem for me. Try that and see the difference. Am using “@angular/cli”: “^1.5.0”, and “webpack”: “^3.8.1”
I am facing same issue. I have angular cli 1.5.0
Angular CLI: 1.5.0 Node: 8.9.0 OS: darwin x64 Angular: 5.0.0 … animations, common, compiler, core, forms, http … language-service, platform-browser, platform-browser-dynamic … platform-server, router
@angular/cli: 1.5.0 @angular/compiler-cli: 4.4.6 @angular/tsc-wrapped: 4.4.6 @angular-devkit/build-optimizer: 0.0.32 @angular-devkit/core: 0.0.20 @angular-devkit/schematics: 0.0.35 @ngtools/json-schema: 1.1.0 @ngtools/webpack: 1.8.0 @schematics/angular: 0.1.0 typescript: 2.6.1 webpack: 3.8.1
Go into
node_modules/webpack/
directory then installenhanced-resolve@3.3.0
work for me.problem
There is no
'enhanced-resolve'
package in mypackage.json
, and not in the root of the node_modules. So I runyarn add enhanced-resolve@3.3.0 --dev
, thenng build --prod
still not work. then I checked package list by runyarn list
, I findenhanced-resolve@3.3.0
inpackage.json
from@angular-cli/webpack
butenhanced-resolve@3.4.1
installed in the webpack. So I go into the webpack directory and force installenhanced-resolve@3.3.0
, then it’s work for me.@IRCraziestTaxi you can put
entryModule
on either your webpack config or your typescript config. Make sure thetsConfigPath
points to the proper location of your tsconfig file. Also, the tsconfig settings are VERY picky, below I copied my entire working file so try that as is before modifying anything.@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.@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:
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:
from @awerlang
@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 @angular/cli@1.2.4 did the trick for me. It has enhanced-resolve@3.3.0 as its dependency.
1.3.0.rc-1
and1.2.4
should fix the issue without trick install ofenhanced -resolve,
but you need to install them withnpm
. Foryarn
users there is another problem apparentlyI’v that problem too, use
ng new
create a project and useng build --prod
,and it fails.@angular/cli: 1.1.3 node: 6.9.5 os: darwin 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.1.3 @angular/compiler-cli: 4.3.1 @angular/language-service: 4.3.1
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.
@russkayairina it’s a normal behavior :
ng build --env=prod : replace environment.ts by environment.prod.ts, but it’s a simple ng build. ng build --prod : improved build with aot + environment.prod.ts replacement (before, it was ng build --prod --aot).
To make your project working with aot, follow these steps : https://medium.com/@isaacplmann/making-your-angular-2-library-statically-analyzable-for-aot-e1c6f3ebedd5#.n4b11wso1
The same thing happens for me. No error other than:
ERROR in ./src/client.aot.ts Module not found: Error: Can't resolve './$$_gendir/browser.module.ngfactory' in '/home/pradej/Projekty/site-backoffice/src' @ ./src/client.aot.ts 2:0-75
I had to make a separate, plain client entry point for AOT, but it still doesn’t work.
OS: Fedora 25 x64
Also, if that’s helpful, my project is using Universal, but AOT is used for client only.