I’m working on an Ionic 3 project, and every time I run ionic serve
, it crashes with the error: FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory.
What could be causing this memory issue, and how can I increase the heap size or optimize my project to prevent this from happening?
I’ve dealt with this a few times in legacy Ionic 3 projects. That error usually means Node’s memory limit is too low for your build process — behind the scenes, Webpack needs more heap space. A straightforward fix is to increase Node’s heap size like this:
export NODE_OPTIONS=--max-old-space-size=4096
ionic serve
Or on Windows:
set NODE_OPTIONS=--max-old-space-size=4096
ionic serve
This bumps it to 4GB, which is often enough, but you can increase it further if your machine allows. Just remember this trick anytime you hit that “ineffective mark-compacts near heap limit allocation failed - JavaScript heap out of memory” error in Ionic 3.
Building on that, from experience, this error often pops up because the project itself is a bit heavy—maybe you’ve got unused dependencies or large asset files weighing it down. Ionic 3 apps tend to accumulate bloat over time. Here’s what I do next:
- Run a production build with
npm run build --prod
to see if the error persists.
- Audit your dependencies and remove any unused npm packages.
- Check if you have oversized images or sourcemaps that can be optimized or removed.
Of course, you’ll still want to keep the increased Node heap size environment variable set while serving. That combination usually clears this “ineffective mark-compacts near heap limit allocation failed - JavaScript heap out of memory” error.
Adding to that, sometimes leftover junk in your node_modules or package-lock.json can cause memory issues, too. From my experience troubleshooting these builds, here’s a reliable cleanup routine:
- Delete your
node_modules
folder and package-lock.json
.
- Run
npm install
fresh to get a clean slate.
- Try
ionic serve
again with the Node heap size increased as before.
Also, make sure your local @ionic/app-scripts
version is up to date—some older versions had notorious memory leaks that triggered this exact “ineffective mark-compacts near heap limit allocation failed - JavaScript heap out of memory” error. These steps usually resolve it once and for all.