8

After upgrading my angular 10 to angular 13, I got this error after using the command ng serve.

⠧ Generating browser application bundles (phase: building)...events.js:377
      throw er; // Unhandled 'error' event
      ^

Error [ERR_WORKER_OUT_OF_MEMORY]: Worker terminated due to reaching memory limit: JS heap out of memory
    at new NodeError (internal/errors.js:322:7)
    at Worker.[kOnExit] (internal/worker.js:276:26)
    at Worker.<computed>.onexit (internal/worker.js:203:20)
Emitted 'error' event on Worker instance at:
    at Worker.[kOnExit] (internal/worker.js:276:12)
    at Worker.<computed>.onexit (internal/worker.js:203:20) {
  code: 'ERR_WORKER_OUT_OF_MEMORY'
}

Already tried to increase the memory limit of Node and installing the latest version of Node.

My node version is v14.18.1 and my npm version is 6.14.15. This problem didn't happen with angular 12 or 11.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
William
  • 91
  • 1
  • 1
  • 4
  • 3
    I up-voted your question because even though Felix says it's been asked many times, it was never asked in relation to Angular or 'npm build'. If not for your question (and Felix's answer), I would still be searching for a solution. – John Pankowicz Dec 21 '21 at 15:59
  • 1
    Thank you for your attention. Still not sure what is causing the problem, maybe I should remove some of the dependencies from package.json, maybe one of them is causing the problem. – William Dec 22 '21 at 16:13
  • Felix's solution solved the issue for me. Do you still get the same error output when you use his build command? Also, did you try increasing max_old_space_size further. See: https://stackoverflow.com/a/48392705/1978840 – John Pankowicz Dec 22 '21 at 21:17
  • What Node sass version are you using ? – Sameh Feb 19 '22 at 03:38

3 Answers3

3

I am sure it was asked and answered many times, but change package.json to define build as

"build": "rimraf dist && node --max_old_space_size=8192 ./node_modules/@angular/cli/bin/ng build"
Felix
  • 9,248
  • 10
  • 57
  • 89
  • Sorry, unfortunately this did not work, I could share my dependency package if it is of any help. – William Dec 22 '21 at 16:10
3

It is very odd that no one is mentioning a clear direct solution to this issue, in my case increasing the node js memory allocation was not the answer

The issue apparently has to do with styles especially when using Sass, there are two things you need to clarify

1- You are using the proper node sass version along with node js version

2-if step one didn't resolve your issue them move to dart sass, Replace any import in sass file to use ! The new @use is similar to @import . but has some notable differences: The file is only imported once, no matter how many times you @use it in a project. Variables, mixins, and functions (what Sass calls "members") that start with an underscore (_) or hyphen (-) are considered private, and not imported

Once I have applied these my issue was gone

Sameh
  • 1,318
  • 11
  • 11
  • how do you do step 1, can you clarify? I don't see anything related to SASS in my package.json – Chris Kooken Jun 28 '22 at 21:47
  • Hi Chris as I explained it was happening for my case, there could be other triggering factors but even thought if you are not using sass examine your css files are the case or not , just try build without it the css to see if it is passing or not, there is no direct answer here it is just trial and error – Sameh Jul 04 '22 at 15:30
0

Incase anyone comes looking for an answer on this thread, I solved it by adding the below in the package.json file -

"scripts": {
  "ng": "node --max_old_space_size=4096 ./node_modules/.bin/ng",
}

Found the answer on GitHub - https://github.com/nrwl/nx/issues/1110

D M
  • 5,769
  • 4
  • 12
  • 27