55

Workaround to fix heap out of memory when running node binaries (which is a common issue when using TypeScript 2.1+ and webpack) is increasing the max mem for node.

increase-memory-limit is a package to do that. In the link, it says

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

But how do I set that environment variable in Windows? In powershell, it is giving me the error "export : The term 'export' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.".

Zag
  • 693
  • 1
  • 5
  • 9

7 Answers7

88

export is a Linux command. You can use set for Windows:

set NODE_OPTIONS=--max-old-space-size=4096

glimmbo
  • 143
  • 10
Peter Boomsma
  • 8,851
  • 16
  • 93
  • 185
  • 47
    how to check this was applied? – godblessstrawberry Oct 14 '19 at 15:16
  • 4
    this will help you v8.getHeapStatistics().total_available_size / 1024 / 1024 as of version 10.14.1 both command line arg and environment variable NODE_OPTIONS work – Andrey Oct 31 '19 at 15:51
  • isn't `set` suppose to persist between sections? I mean, I close my terminal, the problem is already there.... If not, is there a way to get it persisted ? – Davi Daniel Siepmann Feb 26 '20 at 16:26
  • 1
    I think you can use [setx](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/setx) to persist environment variables on Windows Server. It [varies on other versions](https://www.computerhope.com/issues/ch000549.htm). – M Miller Mar 05 '20 at 16:24
  • 1
    After setting it with set NODE_OPTIONS=--max_old_space_size=4096 When I am checking it with: node -e 'console.log(v8.getHeapStatistics().heap_size_limit/(1024*1024))' It have not been increased..... – Dilip Kumar Yadav Jan 08 '22 at 12:58
  • 4
    This answer is wrong please edit to use the syntax of the ` --max-old-space-size` flag as in the documentation, with hyphens instead of underscores: https://nodejs.org/dist/latest-v16.x/docs/api/cli.html#--max-old-space-sizesize-in-megabytes – Milan Jul 11 '22 at 13:27
27

If running with powershell, the command for setting NODE_OPTIONS should be like:

$env:NODE_OPTIONS="--max-old-space-size=8192"

And then you can check whether it's applied with following command:

node -e 'console.log(v8.getHeapStatistics().heap_size_limit/(1024*1024))'
8240
Edward Zhang
  • 441
  • 4
  • 13
1

As per nodeJs documentation, property should be (with hyphens)

--max-old-space-size=SIZE (in megabytes)

and can be used in start script or set/export with NODE_OPTIONS like others said. No need of new library to import for this.

$ node --max-old-space-size=800 index.js

Reference: https://nodejs.org/api/cli.html#cli_max_old_space_size_size_in_megabytes

NKS
  • 47
  • 1
  • 8
0

You can use the following command in powershell to increase the node memory:

$Env:NODE_OPTIONS = "--max-old-space-size=4096"

After that you should not have problems with your application.

0

Firstly, I would like to thank the writer(Edward Zhang) who made a comment 2 comment above. I decided to write this comment.

I got this error while I am working with angular application. There are some solutions to solve this, but most of them did not work for me. But finally I found a solution. By using environment variable:

enter image description here

Check environment variable on node.js cmd by using this command.

v8.getHeapStatistics()

If environment variable for solving this issue does not work, you can use this command inside vs code.

$env:NODE_OPTIONS="--max-old-space-size=4096"

You can check this:

node -e 'console.log(v8.getHeapStatistics().heap_size_limit/(1024*1024))'

After this command I got some errors like some modules could not found vs. For fixing this error you can run this commands

npm clean-install
npm install

If heap-allocation error continue, you can increase volume like this way.

--max-old-space-size=1024 index.js #increase to 1gb
--max-old-space-size=2048 index.js #increase to 2gb
--max-old-space-size=3072 index.js #increase to 3gb
--max-old-space-size=4096 index.js #increase to 4gb
--max-old-space-size=5120 index.js #increase to 5gb
--max-old-space-size=6144 index.js #increase to 6gb
--max-old-space-size=7168 index.js #increase to 7gb
--max-old-space-size=8192 index.js #increase to 8gb
Mesut KAYA
  • 35
  • 6
0

If you work in a team or in open-source, make sure you use cross-env so that your command works everywhere, you can place the command that needs the ENV right after it. For example:

cross-env NODE_OPTIONS="--max_old_space_size=4096" node index.js

or

cross-env NODE_OPTIONS="--max_old_space_size=4096" webpack --mode=production

To install it:

npm install cross-env --save-dev

cross-env is the de-facto standard for this operation in Node: https://www.npmjs.com/package/cross-env

fregante
  • 29,050
  • 14
  • 119
  • 159
-5

To resolve this issue on AWS Instance,

Login into AWS Console and turn of the instance from the Instance State tap

Then under the Action tap

Select change instance type

Then choose t2.medium, save and exit

Go back and start then instance

Then run your app again

This common on t2.micro instance cause it is 1 gig Ram

Thanks