angular / angular-cli

CLI tool for Angular
https://cli.angular.io
MIT License
26.63k stars 11.98k forks source link

ng build -prod Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory' #4551

Closed thaniri closed 7 years ago

thaniri commented 7 years ago

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.

agarbutt commented 7 years ago

Having the same failure on both linux and windows platforms.

ERROR in Cannot read property 'map' of undefined

ERROR in ./src/main.ts
Module not found: Error: Can't resolve './$$_gendir/app/helix-app.module.ngfactory' in 'C:\Users\agarbutt\WebstormProjects\helix-ui\src'
 @ ./src/main.ts 3:0-85
 @ multi ./src/main.ts

main.ts

import {platformBrowserDynamic} from "@angular/platform-browser-dynamic";
import {enableProdMode} from "@angular/core";
import {environment} from "./environments/environment";
import {HelixAppModule} from "./app/";

if (environment.production) {
  enableProdMode();
}

platformBrowserDynamic()
  .bootstrapModule(HelixAppModule)
  .catch(err => console.error(err));
jjaskulowski commented 7 years ago

+1

adessilly commented 7 years ago

+1 (on Windows 10 64bits)

IlyaSurmay commented 7 years ago

+1 linux mint

ERROR in this._input.charCodeAt is not a function

ERROR in ./src/main.ts
Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory' in '/home/vs/Work/GoFactTables/GoFactFrontend/src'
 @ ./src/main.ts 5:0-74
 @ multi ./src/main.ts
mhamel06 commented 7 years ago

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:

ERROR in Child compilation failed:
Module build failed: No input specified: provide a file name or a source string to process:
[object Object]

ERROR in ./src/main.ts
Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory' in '/Users/mhamel/Development/blispay/blispay-ui2/src'
 @ ./src/main.ts 4:0-76
 @ multi ./src/main.ts

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?

thaniri commented 7 years ago

http://pastebin.com/Hmrs0Kh7

CTRL+F the document for 'was generated' to find out what command I used: ng build -prod --verbose > aFile.txt ng build -prod --verbose

If you want to try it yourself please clone this repo: https://github.com/thaniri/angularPortfolio

I have no empty .scss files, and even if I attempt to add something to styles.css in the src folder, I get the same error.

TheWalruzz commented 7 years ago

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.

import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { MainModule } from './browser.module';

enableProdMode();

platformBrowserDynamic().bootstrapModule(MainModule);

OS: Fedora 25 x64

Also, if that's helpful, my project is using Universal, but AOT is used for client only.

jjaskulowski commented 7 years ago

I think it might be ngc. I cannot make it generate anything even when running it directly.

16.02.2017 4:43 PM "Ilya Surmay" notifications@github.com napisał(a):

+1 linux mint

ERROR in this._input.charCodeAt is not a function

ERROR in ./src/main.ts Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory' in '/home/vs/Work/GoFactTables/GoFactFrontend/src' @ ./src/main.ts 5:0-74 @ multi ./src/main.ts

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/angular/angular-cli/issues/4551#issuecomment-280366544, or mute the thread https://github.com/notifications/unsubscribe-auth/ATNfaOp3L5J0yev2EYWiQmUg8iZvNSx_ks5rdG6GgaJpZM4L7u-Y .

mayachirp commented 7 years ago

Same error when I upgraded to 4.0.0-beta.8. Previous version 4.0.0-beta.7 doesn't have this failure.

adessilly commented 7 years ago

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

nikoxgon commented 7 years ago

See next. I found a solution for me.

I hope this helps.

pbalaga commented 7 years ago

Similar problem with 1.0.0-rc1 on Windows. Running ng build --prod gives

ERROR in Error encountered resolving symbol values statically. Could not resolve events relative to C:/Projects/craft-ng/editor/node_modules/blocking-proxy/built/lib/webdriver_commands.d.ts., resolving symbol WebDriverCommand in C:/Projects/craft-ng/editor/node_modules/blocking-proxy/built/lib/webdriver_commands.d.ts

ERROR in ./src/main.ts Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory' in 'C:\Projects\craft-ng\editor\src' @ ./src/main.ts 4:0-74 @ multi ./src/main.ts

ng build --prod --aot=false executes without problems.

TheWalruzz commented 7 years ago

This issue was closed, any advice for people still sufferring from this error?

TheWalruzz commented 7 years ago

Just to tell everyone: I eventually dropped @ngtools/webpack completely from my project and replaced it with @ultimate/aot-loader. AOT now finally works.

WiL-dev commented 7 years ago

Instead of use ng build --prod use ng 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

pbalaga commented 7 years ago

@WiL-dev : I can confirm it works fine on 1.0.0-rc1. Thanks!

hippee-lee commented 7 years ago

@WiL-dev - I'm not sure ng build --env-prod builds with aot like ng build --prod does. If I run ng build --env=prod the tool generates the same size for my output as ng build --env=dev does. In addition running ng build --env=prod --aot=true gnerates the same error that I was origially getting. Unexpected value 'null' exported by the module 'MyModule' && down the stack Error: 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.

russkayairina commented 7 years ago

unresolved, --env produces not the same output as --prod

adessilly commented 7 years ago

@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

dhilgarth commented 7 years ago

This is still a problem. It happens even with the newly created project from ng new!

Full output:

d:\Temp\cli>ng -v
    _                      _                 ____ _     ___
   / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
  / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
 / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
/_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
               |___/
@angular/cli: 1.0.0
node: 6.9.1
os: win32 x64

d:\Temp\cli>ng new cli-test
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
Successfully initialized git.
Installing packages for tooling via npm.
Installed packages for tooling via npm.
Project 'cli-test' successfully created.

d:\Temp\cli>cd cli-test

d:\Temp\cli\cli-test>ng build --prod --env=prod
Hash: 0bfde11b73d9b21b101b
Time: 6189ms
chunk    {0} polyfills.7fd9b43f06a670d8299a.bundle.js (polyfills) 158 kB {3} [initial] [rendered]
chunk    {1}  (styles) 1.72 kB {3} [initial] [rendered]
chunk    {2} main.e1bb4892ef6fedc166cd.bundle.js (main) 806 kB [initial] [rendered]
chunk    {3} inline.65cd0835207a2ba3e811.bundle.js (inline) 0 bytes [entry] [rendered]

ERROR in E:/Temp/cli/cli-test/src/main.ts
Module not found: Error: Can't resolve './d:/Temp/cli/cli-test/src/$$_gendir/app/app.module.ngfactory' in 'E:\Temp\cli\cli-test\src'
 @ E:/Temp/cli/cli-test/src/main.ts 4:0-99
 @ multi ./src/main.ts

d:\Temp\cli\cli-test>
LancerComet commented 7 years ago

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!
  //...
}
warley996 commented 7 years ago

Very useful @WiL-dev your comment works fine for me. 4.0.0

waratah commented 7 years ago

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.

altschuler commented 7 years ago

@waratah How did you track down the error?

waratah commented 7 years ago

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

kmui2 commented 7 years ago

fixed my error with AppModule not a NgModule by updating typescript

npm install --save typescript@latest `

wassimsidia commented 6 years ago

try to add aot option, so build command will be like this : ng build -prod --aot=false

altschuler commented 6 years ago

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.

CanKattw commented 6 years ago

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.

WiL-dev commented 6 years ago

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.

Migration guide

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.

agarbutt commented 6 years ago

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.

scottieslg commented 6 years ago

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.

warmans commented 6 years ago

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
KhalipskiSiarhei commented 6 years ago

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

alexeykostevich commented 6 years ago

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

alejandromav commented 6 years ago

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

warmans commented 6 years ago

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.

jakeNiemiec commented 6 years ago

@warmans beat me to it 👍. Hopfully @thaniri can re-open this

enjoiful commented 6 years ago

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?

VivaLaPanda commented 6 years ago

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.

zxhfighter commented 6 years ago

I'v that problem too, use ng new create a project and use ng build --prod,and it fails.

ERROR in ./src/main.ts
Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory' in '/Users/baidu/apps/device-monitor/src'
 @ ./src/main.ts 3:0-74
 @ multi ./src/main.ts

@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

tianyuanzhonglu commented 6 years ago

@angular/cli: 1.2.4 fix the problem

abhi901abhi commented 6 years ago

@angular/cli: 1.2.4 fix the problem

flauc commented 6 years ago

I'm getting this error but I'm not using @angular/cli.
npm install enhanced-resolve@3.3.0 Does fix it temporarily, but does someone know how angular/cli fixed it?

eydrian commented 6 years ago

@angular/cli: 1.2.4 doesn't fix the problem for me. npm install enhanced-resolve@3.3.0 fixes it temporarily

BaimosTechnologies commented 6 years ago

@angular/cli: 1.2.4 didn't fix it. enhanced-resolve: 3.3.0 fixes it.

flauc commented 6 years ago

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.

swordman1205 commented 6 years ago

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.

mhamel06 commented 6 years ago

https://github.com/angular/angular-cli/commit/dbc7959503604af077c0fa1ada4d60643e5ab399 Fixes it by pinning enhanced-resolve@3.3.0

vcorr commented 6 years ago

Fixed by enhanced-resolve@3.3.0 on Mac