'Worker terminated due to reaching memory limit: JS heap out of memory

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.



Solution 1:[1]

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"

Solution 2:[2]

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

Solution 3:[3]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Felix
Solution 2
Solution 3 D M