Angular AOT / Webpack memory trick

If you're dealing with a growing javascript app, you might reach that point where your build scripts will run out of memory. Wait what? I have 16 GB on my machine, how can this happen?

Update 15/03/2018

Starting with node 8, you should be able to use the NODE_OPTIONS environment variable to define your preferences globally.

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

The problem

Let say for instance that you are using Rollup with a set of very common plugins such as:

rollup-plugin-node-builtins

rollup-plugin-node-globals

rollup-plugin-node-resolve

rollup-plugin-commonjs

rollup-plugin-uglify

You now write a lot of code and add a bunch of 3rd party libs. You're happy everything works fine. Proud and ready to commit you try to bundle and uglify the thing using your home made shiny npm run build:prod or whatever script you use to do that. And Boom!

OK ok, but javascript world is what it is and for the sake of getting things done, what can you do?

Solution

The core problem is that node comes with a default memory restriction to 1.76 GB. If you need more you need to set the option --max_old_space_size={desiredSize} when you start a node process.

Wait! What? I'm running an npm command how do I pass an option to node? Well you don't!

Since npm does not provide the ability to set this option, we have to do something else. If you are using bash or a similar you can use aliases. If you are running on windows, you're life is gonna be harder.