angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

AOT build fails due to "JavaScript heap out of memory"

Open ghost opened this issue 9 years ago • 427 comments

Bug Report or Feature Request (mark with an x)

- [X ] bug report -> please search issues before submitting
- [ ] feature request

Versions.

@angular/cli: 1.0.0
node: 7.7.4
os: win32 x64
@angular/animations: 4.0.0
@angular/common: 4.0.0
@angular/compiler: 4.0.0
@angular/core: 4.0.0
@angular/flex-layout: 2.0.0-rc.1
@angular/forms: 4.0.0
@angular/http: 4.0.0
@angular/material: 2.0.0-beta.2
@angular/platform-browser: 4.0.0
@angular/platform-browser-dynamic: 4.0.0
@angular/router: 4.0.0
@angular/cli: 1.0.0
@angular/compiler-cli: 4.0.0

Repro steps.

ng build --prod --aot

The log given by the failure.

/node_modules/@angular/material/aut 92% chunk asset optimization
<--- Last few GCs --->

[6328:0000016AB4D09F00]  1775597 ms: Mark-sweep 1411.2 (1513.5) -> 1411.0 (1513.5) MB, 1240.6 / 0.0 ms  last resort


<--- JS stacktrace --->

==== JS stack trace =========================================

Security context: 0000034B0DD266A1 <JS Object>
    2: optimize [0000000FABC02311 <undefined>:5480] [pc=00000082575652C8](this=000000633199A279 <an AST_Call with map 0000014753FA0291>,compressor=00000226DBDECA61 <a Compressor with map 0000032B1EC8F829>)
    3: before [0000000FABC02311 <undefined>:5463] [pc=0000008258C1818D](this=00000226DBDECA61 <a Compressor with map 0000032B1EC8F829>,node=000000633199A279 <an AST_Call with map 0000014753...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

Desired functionality.

I would like to be able the app using AOT.

Mention any other details that might be useful.

I have tried changing ng.cmd to this:

@IF EXIST "%~dp0\node.exe" (
  "%~dp0\node.exe" --max_old_space_size=8192 "%~dp0\..\@angular\cli\bin\ng" %*
) ELSE (
  @SETLOCAL
  @SET PATHEXT=%PATHEXT:;.JS;=;%
  node --max_old_space_size=8192 "%~dp0\..\@angular\cli\bin\ng" %*
)

and ngc.cmd to:

@IF EXIST "%~dp0\node.exe" (
  "%~dp0\node.exe" --max_old_space_size=8192 "%~dp0\..\@angular\compiler-cli\src\main.js" %*
) ELSE (
  @SETLOCAL
  @SET PATHEXT=%PATHEXT:;.JS;=;%
  node --max_old_space_size=8192 "%~dp0\..\@angular\compiler-cli\src\main.js" %*
)

I have 8GB swap file. The build fails after 20-25 minutes standing on 92% progress.

ghost avatar Mar 24 '17 09:03 ghost

I've had the same issue and the only way I have solved it is to unfortunately use this flag: --build-optimizer=false

joshuwhi avatar Jul 26 '18 14:07 joshuwhi

@Marudhuraj Other than turning off build-optimizer, turning off source maps can also help. Would otherwise also in general recommend updating to latest angular-cli to get the best performance as well as ensure you're on Node version 8.11.2 or higher as there's a performance regression affecting Webpack builds of some versions below that in the 8.x series.

Koslun avatar Jul 26 '18 15:07 Koslun

@joshuwhi @Koslun My personal experience was I was encountering this issue after 5 or more builds of the same app in succession on a team city agent. I have an opportunity to set up my CI on the latest Node, NPM, and Angular and will find some time in the next week or so to set up just the boilerplate ng new app building periodically throughout the day to see if the suggested dev environment versions mentioned here actually no longer reproduce the issue.

vandensd avatar Jul 26 '18 15:07 vandensd

@Marudhuraj I solved this issue by doing the following step:

  1. I used the command "npm run build" which internally uses "ng build"
  2. I update my "ng.cmd" file at following location "\UI\node_modules.bin" (UI being the project folder) by this code ================= @IF EXIST "%~dp0\node.exe" ( "%~dp0\node.exe" --max_old_space_size=8192 "%~dp0..@angular\cli\bin\ng" %* ) ELSE ( @SETLOCAL @SET PATHEXT=%PATHEXT:;.JS;=;% node --max_old_space_size=8192 "%~dp0..@angular\cli\bin\ng" %* ) ================

this is basically adding more memory for node to execute
3. Update the package.json file, by doing following steps: replace "build": "ng build", by this: "build": "ng build --prod --build-optimizer",

Thanks Vijay

aryavijay avatar Jul 26 '18 15:07 aryavijay

Hi @aryavijay , Now i'm getting this error, how to resolve the app module factory error

ERROR in ./src/app/app.module.ngfactory.js Module not found: Error: Can't resolve 'ngx-store/dist/src/service/local-storage.service' in 'C:\Users\a811891\taskmanagementv2ui\src\app' resolve 'ngx-store/dist/src/service/local-storage.service' in 'C:\Users\a811891\taskmanagementv2ui\src\app' Parsed request is a module using description file: C:\Users\a811891\taskmanagementv2ui\package.json (relative path: ./src/app) Field 'browser' doesn't contain a valid alias configuration after using description file: C:\Users\a811891\taskmanagementv2ui\package.json (relative path: ./src/app) resolve as module C:\Users\a811891\taskmanagementv2ui\src\app\node_modules doesn't exist or is not a directory C:\Users\a811891\taskmanagementv2ui\src\node_modules doesn't exist or is not a directory C:\Users\a811891\node_modules doesn't exist or is not a directory C:\Users\node_modules doesn't exist or is not a directory C:\node_modules doesn't exist or is not a directory looking for modules in C:\Users\a811891\taskmanagementv2ui\node_modules using description file: C:\Users\a811891\taskmanagementv2ui\package.json (relative path: ./node_modules) Field 'browser' doesn't contain a valid alias configuration after using description file: C:\Users\a811891\taskmanagementv2ui\package.json (relative path: ./node_modules) using description file: C:\Users\a811891\taskmanagementv2ui\node_modules\ngx-store\package.json (relative path: ./dist/src/service/local-storage.service) no extension Field 'browser' doesn't contain a valid alias configuration C:\Users\a811891\taskmanagementv2ui\node_modules\ngx-store\dist\src\service\local-storage.service doesn't exist .ts

Marudhuraj avatar Jul 27 '18 06:07 Marudhuraj

I'm a noob, but this solved my problem,

Instead of: ng build --prod

I wrote: ng build --env prod

and it worked...

Please inform me if this answere is not good an why so i can expand my knowledge :) Thanks.

yaadm avatar Aug 06 '18 10:08 yaadm

@yaadm I think the --env only sets the environment.ts file, it doesn't set all the other production mode settings that --prod sets.

kdubious avatar Aug 06 '18 14:08 kdubious

With Angular 6 the build time up from 600 seconds to 900 :disappointed:

williangd avatar Aug 13 '18 18:08 williangd

I have the same issue.

ng build --configuration=production

with a production configuration that looks like this

"production": {
          "optimization": true,
          "outputHashing": "all",
          "sourceMap": true,
          "extractCss": true,
          "namedChunks": false,
          "aot": true,
          "extractLicenses": true,
          "vendorChunk": false,
          "buildOptimizer": true,
          "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.prod.ts"
            }
          ]
        }

fails with

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

But setting the flag: --max_old_space_size=8192 makes it work.

node --max_old_space_size=8192 ./node_modules/@angular/cli/bin/ng build --configuration=production

Node version: v8.11.3 Npm version: 6.3.0

Moulde avatar Aug 14 '18 13:08 Moulde

any solution to this ? ....

liangwenzhong avatar Aug 20 '18 04:08 liangwenzhong

read my comment or screenshare.....

aryavijay avatar Aug 20 '18 04:08 aryavijay

@aryavijay I feel like setting max_old_space_size in various places is not really a solution, but rather a workaround. This was not an issue before I upgraded the project and cli to angular 6.

Moulde avatar Aug 20 '18 08:08 Moulde

I am having the same issue, but it only occurs if I have "sourceMap": true, in angular.json, otherwise it builds just fine.

paustint avatar Aug 21 '18 04:08 paustint

I've just hit into this issue as well after upgrading to v6. It was working fine before. ):

alfaproject avatar Aug 22 '18 16:08 alfaproject

Is anyone else having problem with the build in 79% ModuleConcatenationPlugin? It's taking a long time and consuming a lot of memory.

williangd avatar Aug 22 '18 19:08 williangd

This issue makes us unable to upgrade @angular-devkit/build-angular from 0.6 to either 0.7 or 0.8.

What is even worse, all mentioned workarounds (buildOptimizer: false, sourceMap: false, --max_old_space_size=8192) do not work for our project.

On "@angular-devkit/build-angular": "0.6.3" everything works fine.

dirkluijk avatar Sep 09 '18 18:09 dirkluijk

I successfully build and serve with

node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng serve --aot

but when i open my browser i got an error

ERROR Error: Uncaught (in promise): Error: Cannot find module 'app/pages/login/login.module'. Error: Cannot find module 'app/pages/login/login.module'

with this version Angular CLI: 1.5.2 Node: 6.10.3 @angular/.DS_Store: error @angular/cdk: 5.2.5 @angular/cli: 1.5.2 @angular-devkit/build-optimizer: 0.0.42 @angular-devkit/core: 0.8.1 @angular-devkit/schematics: 0.0.52 @ngtools/json-schema: 1.1.0 @ngtools/webpack: 1.8.2 @schematics/angular: 0.1.17 typescript: 2.4.2 webpack: 3.8.1

i am tired with changing url in modules and changing versions last 48 hours. please help me.

iyashiyas avatar Sep 14 '18 11:09 iyashiyas

@iyashiyas your issue seems unrelated to memory issues.

dirkluijk avatar Sep 14 '18 11:09 dirkluijk

@iyashiyas your issue seems unrelated to memory issues.

without node --max_old_space_size=8192 am getting error JavaScript heap out of memory

iyashiyas avatar Sep 14 '18 11:09 iyashiyas

@iyashiyas yes, so the workaround works well in your case and the "Cannot find module" error has nothing to do with this.

dirkluijk avatar Sep 14 '18 12:09 dirkluijk