Mohammad Daliri
Mohammad Daliri

Reputation: 1386

Angular FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed

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

Upvotes: 7

Views: 29175

Answers (8)

mustafatorun
mustafatorun

Reputation: 324

I was facing this problem in github actions. I am using dockerfile for building image. I finally found where should I write the "node --max_old_space_size=8000" command. I wrote "RUN node --max_old_space_size=8000 ./node_modules/@angular/cli/bin/ng build --prod" instead of "RUN ng build --prod" in DOCKERFILE

Upvotes: 0

wbartussek
wbartussek

Reputation: 2008

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.

Upvotes: 0

Yaren Çelik
Yaren Çelik

Reputation: 21

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

Upvotes: 2

Marc
Marc

Reputation: 2410

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

Upvotes: 0

update node.js to 12 version help me

Upvotes: 1

Tuncay Uyar
Tuncay Uyar

Reputation: 51

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" %* 
)

Upvotes: 5

user10747134
user10747134

Reputation:

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.

Upvotes: 28

Ankii32
Ankii32

Reputation: 106

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.

Upvotes: 5

Related Questions