6

I've used Angular 5
I try ng build --prod
show this error:
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

I've read this and this and change -max_old_space_size in package.json but does not build production show above error again.
What's the problem? How can solve it? Is the problem of package.json؟

package.json:

{
  "name": "asd",
  "version": "0.6.2",
  "license": "MIT",
  "angular-cli": {},
  "scripts": {
    "build:dev": "./node_modules/.bin/ng build",
    "build:prod": "./node_modules/.bin/ng build --prod --aot=false",
    "build:aot": "./node_modules/.bin/ng build --prod --aot",
    "build:aot2": "node --max_old_space_size=4096 ./node_modules/.bin/ng build --prod --aot",
    "build": "npm run build:dev",
    "clean:dist": "npm run rimraf -- dist",
    "clean:install": "npm set progress=false && npm install",
    "clean:start": "npm start",
    "clean": "npm cache clean && npm run rimraf -- node_modules doc coverage dist",
    "e2e:live": "npm run e2e -- --elementExplorer",
    "e2e": "npm run protractor",
    "lint": "npm run tslint \"src/**/*.ts\"",
    "prebuild:dev": "npm run clean:dist",
    "prebuild:prod": "npm run clean:dist",
    "preclean:install": "npm run clean",
    "preclean:start": "npm run clean",
    "protractor": "protractor",
    "rimraf": "rimraf",
    "server:dev": "./node_modules/.bin/ng serve",
    "serve": "npm run server:dev",
    "start": "npm run server:dev",
    "test": "./node_modules/.bin/ng test",
    "tslint": "tslint",
    "typedoc": "typedoc",
    "ng": "ng",
    "pree2e": "webdriver-manager update --standalone false --gecko false"
  },
  "private": true,
  ....
}
naveen
  • 51,042
  • 46
  • 158
  • 241
Mohammad Daliri
  • 1,240
  • 6
  • 19
  • 40

7 Answers7

24

Upgrading Angular at this time will not solve the issue (but may in the future). I get it with my Angular 7 and 8 projects as well on my Windows machine.

It looks like you need might to update your memory increase in the package.json. Here is a link that seems to have solved it for a lot of people already, I think you have to specify the path to Angular CLI within node modules.

Instead of

node --max_old_space_size=4096 ./node_modules/.bin/ng build --prod --aot

Try

node --max_old_space_size=4096 node_modules/@angular/cli/bin/ng build --prod --aot

This path in the solution looks correct back to Angular 4.

  • Many Thanks, I wanted to compile the Angular project. And I got this error. But with your guidance, the problem was solved. node --max_old_space_size=4096 node_modules/@angular/cli/bin/ng serve --host 0.0.0.0 – Mahdi Mar 02 '21 at 07:29
3

Run following command in cmd prompt :

node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --prod

i believe this, will fix your problem.

Ankii32
  • 76
  • 3
3

First way:

Run as:

node --max_old_space_size=8048 ./node_modules/@angular/cli/bin/ng serve

where 8048 is new memory limit in megabytes (by default it's ~1500). Or add the command to package.json file:

"build-serve": "node --max_old_space_size=8048 ./node_modules/@angular/cli/bin/ng serve"

and run as npm run build-serve

Or another way: The quick and effective solution for Windows

Open C:\Users\userName\%AppData%\Roaming\npm

Copy/paste the following code into your ng.cmd:

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

update node.js to 12 version help me

1

export NODE_OPTIONS=--max-old-space-size=8192

This helped me on Linux. I found this solution on: https://support.snyk.io/hc/en-us/articles/360002046418-JavaScript-heap-out-of-memory

0

No single solution above worked for me. However, the combination below solved the problem:

  1. Update Node.js to latest version (v.12.19.0 LTS in my case)
  2. Build the app with increased JS heap limit allocation, by running the following command: node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --prod
Marc
  • 1,386
  • 1
  • 10
  • 15
0

Had the same problem with Angular 12. In this case running

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

helped.

wbartussek
  • 1,506
  • 1
  • 9
  • 7