Apurv Chaudhary
Apurv Chaudhary

Reputation: 1795

Allocation failed - JavaScript heap out of memory in Angular 11 while ng serve

  1. I have upgraded my Angular version from 5.2 to 11.2
  2. And after upgrading Angular & all library when i start server (ng serve) getting Allocation failed - JavaScript heap out of memory
  3. It also generates 1 file which has information, but i didn't get

can you please help me out of this.

enter image description here

enter image description here

  1. Angular version & details enter image description here

enter image description here

Upvotes: 15

Views: 51484

Answers (8)

Chandan Soni
Chandan Soni

Reputation: 1

"FATAL ERROR" occurs when uninstalling any packages. Try to run this command in your terminal.

npm cache clean -f

The npm cache can sometimes lead to issues, such as outdated or corrupt cached data, causing unexpected behavior during package installation or updates.

Upvotes: 0

MOHAMMED FISAL C
MOHAMMED FISAL C

Reputation: 49

The problem node.js process consuming more memory over time , so increased the memory allocation for the Node.js process. which can be helpful for handling larger applications or projects with heavy memory usage.

The command used for : "node --max-old-space-size=8192 node_modules/@angular/cli/bin/ng serve"

Upvotes: 0

Bogdan
Bogdan

Reputation: 2042

As of Node.js v8.0 shipped August 2017, you can use the NODE_OPTIONS environment variable to set the max_old_space_size globally

export NODE_OPTIONS=--max_old_space_size=4096

@see https://www.npmjs.com/package/increase-memory-limit

Upvotes: 2

eladr
eladr

Reputation: 385

After tried all solutions above and in other threads, i realized i installed 32 bit node js instead of 64bit (angular 16 but the problem is not angular version related)

Upvotes: 3

Ankit Aranya
Ankit Aranya

Reputation: 970

GIT BASH: ENV NODE_OPTIONS=--max_old_space_size=8048 && ng build

CMD SET NODE_OPTIONS=--max_old_space_size=8048 && ng build

Upvotes: 1

Bjarne Gerhardt-Pedersen
Bjarne Gerhardt-Pedersen

Reputation: 1144

I found this error comes from the generation of source maps. Disabling source maps will make the error go away.

Since most need source maps, using the solution for increase memory in npm scripts instead of disabling source maps, solves it.

example:

scripts: {
  "build": "node --max_old_space_size=8192 ./node_modules/@angular/cli/bin/ng build",
  "serve": "node --max_old_space_size=8192 ./node_modules/@angular/cli/bin/ng serve"
}

Upvotes: 9

Raphaël
Raphaël

Reputation: 3726

You can allocate more memory for your build/start task in order to compile:

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

And, according to an answer to this question: "export 'DOCUMENT' was not found in '@angular/platform-browser', DOCUMENT has been moved to @angular/common. So you also have to change your imports for your code to compile.

Upvotes: 3

user10361591
user10361591

Reputation:

Set an environment variable:

SET NODE_OPTIONS=--max_old_space_size=8048

Upvotes: 11

Related Questions