Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jackFlick's avatar

npm run build fails in Forge production - FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory

I'm trying to deploy my Laravel with Inertia, Vue and TailwindCSS. I'm using AWS EC2. When I tried to run npm run build I am getting this error message:

vite v4.5.2 building for production...
transforming...

/images/webp/honeycomb-bg.webp referenced in /home/forge/default/resources/css/app.css didn't resolve at build time, it will remain unchanged to be resolved at runtime

<--- Last few GCs --->

[3021:0x68e9870]    23669 ms: Mark-sweep 476.4 (493.7) -> 473.6 (494.9) MB, 832.5 / 0.0 ms  (average mu = 0.228, current mu = 0.034) allocation failure; scavenge might not succeed
[3021:0x68e9870]    24664 ms: Mark-sweep 477.7 (494.9) -> 475.0 (496.2) MB, 943.7 / 0.0 ms  (average mu = 0.138, current mu = 0.052) allocation failure; scavenge might not succeed


<--- JS stacktrace --->

FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
 1: 0xb95b60 node::Abort() [node]
 2: 0xa9a7f8  [node]
 3: 0xd6f2f0 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
 4: 0xd6f697 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
 5: 0xf4cba5  [node]
 6: 0xf5f08d v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
 7: 0xf3978e v8::internal::HeapAllocator::AllocateRawWithLightRetrySlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]
 8: 0xf3ab57 v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]
 9: 0xf1bd2a v8::internal::Factory::NewFillerObject(int, v8::internal::AllocationAlignment, v8::internal::AllocationType, v8::internal::AllocationOrigin) [node]
10: 0x12e114f v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [node]
11: 0x170deb9  [node]
Aborted (core dumped)

I have already tried running command and adding this to deployment script node --max-old-space-size=4096 ./node_modules/vite/bin/vite.js build and adding NODE_OPTIONS=--max-old-space-size=3072 to .env and the server is timing out causing the ssh port 22 to lose connection and in order to get connected again I had to restart both Forge server and AWS instance.

I have also tried using Rollup to manualChunk, manually specifying chunkSize limit in vite.config.js using this:

build: {
        rollupOptions: {
            output:{
                manualChunks(id) {
                    if (id.includes('node_modules')) {
                        return id.toString().split('node_modules/')[1].split('/')[0].toString();
                    }
                },
            },
        },
        chunkSizeWarningLimit: 2700,
    },

Also tried using the vite-plugin-chunk-split package, but everything still not working.

In my local machine when building the npm there's one package that is too large public/build/assets/index-af918e0f.js 2,689.81 kB │ gzip: 404.2 and i needed it for most of my vue components.

I know there's a way to fix this but right now I'm empty handed and needed some help with the experts.

Thank you in advance!

0 likes
3 replies
LaryAI's avatar
Level 58

It seems like you're encountering an out-of-memory issue when trying to build your project with Vite on your AWS EC2 instance. This can happen when the build process requires more memory than what is available on the machine. Here are a few steps you can take to try and resolve this issue:

  1. Increase the Memory of the EC2 Instance: If you're running on a small instance, you might simply need more RAM. Consider upgrading to a larger instance type with more memory.

  2. Optimize Your Build: If increasing the memory isn't an option or doesn't solve the issue, you can try to optimize your build process. This can include:

    • Breaking down large components into smaller chunks.
    • Analyzing your bundle to remove or split heavy dependencies.
    • Using dynamic imports to lazy-load parts of your application.
  3. Increase Node's Memory Limit: You've already tried increasing the memory limit, but it's possible that the limit wasn't applied correctly. Make sure to set the NODE_OPTIONS environment variable before running the build command. You can do this by running the following command in your SSH session before starting the build:

    export NODE_OPTIONS=--max-old-space-size=4096
    npm run build
    

    Alternatively, you can modify your package.json scripts to include the memory limit:

    "scripts": {
      "build": "node --max-old-space-size=4096 node_modules/vite/bin/vite.js build"
    }
    
  4. Check for Memory Leaks: Sometimes, a memory leak in your code or one of the dependencies can cause out-of-memory issues. Use tools like the Chrome Developer Tools to profile your application and look for leaks.

  5. Splitting Vendors: Your current manual chunking strategy seems to be on the right track. However, you might want to refine it further by splitting out more chunks if possible, especially for large libraries.

  6. Server Configuration: Ensure that your server configuration isn't limiting the amount of memory that processes can use. Check the ulimit settings and other related configurations.

  7. Build Locally and Upload: As a last resort, if you're unable to resolve the memory issues on the server, you could build the project on your local machine (assuming it has sufficient resources) and then upload the built assets to your server.

Here's an example of how you might adjust your vite.config.js to further optimize chunk splitting:

export default {
  build: {
    rollupOptions: {
      output: {
        manualChunks(id) {
          if (id.includes('node_modules')) {
            // Split each node_modules dependency into its own chunk
            return `vendor_${id.toString().split('node_modules/')[1].split('/')[0]}`;
          }
        },
        // Consider setting a max file size to split larger chunks
        chunkFileNames: 'assets/[name]-[hash].js',
      },
    },
    // Adjust the chunk size warning limit if necessary
    chunkSizeWarningLimit: 500, // This value is in KB
  },
};

Remember to replace 500 with the appropriate chunk size warning limit for your project.

If none of these solutions work, consider reaching out to the Vite community or checking the Vite GitHub issues for similar problems and potential solutions. It's also worth checking if there are any known issues with the specific version of Vite or Node.js you are using.

1 like

Please or to participate in this conversation.