Question

App Platform Node.js Service Heap Out Of Memory

I’m running a Node.js API as a service and while historically it ran perfectly fine. But recently it keeps failing with this error:

[2021-07-27 23:31:06] 
[2021-07-27 23:31:06] INFO[0045] Taking snapshot of full filesystem...        
[2021-07-27 23:31:41] INFO[0080] Pushing layer <registry-uri-3> to cache now 
[2021-07-27 23:31:41] INFO[0080] Pushing image to <registry-uri-4> 
[2021-07-27 23:31:41] INFO[0081] COPY . .                                     
[2021-07-27 23:31:41] INFO[0081] Taking snapshot of files...                  
[2021-07-27 23:31:41] INFO[0081] ENV NODE_ENV production                      
[2021-07-27 23:31:41] INFO[0081] No files changed in this command, skipping snapshotting. 
[2021-07-27 23:31:41] INFO[0081] RUN npm run build                            
[2021-07-27 23:31:41] INFO[0081] cmd: /bin/sh                                 
[2021-07-27 23:31:41] INFO[0081] args: [-c npm run build]                     
[2021-07-27 23:31:41] INFO[0081] Running: [/bin/sh -c npm run build]          
[2021-07-27 23:31:42] 
[2021-07-27 23:31:42] > api@1.0.0 build /app
[2021-07-27 23:31:42] > tsc
[2021-07-27 23:31:42] 
[2021-07-27 23:32:19] INFO[0119] Pushed image to 1 destinations               
[2021-07-27 23:32:30] 
[2021-07-27 23:32:30] <--- Last few GCs --->
[2021-07-27 23:32:30] 
[2021-07-27 23:32:30] [213:0x55d1a5f5ee40]    46077 ms: Mark-sweep (reduce) 1019.1 (1025.9) -> 1018.2 (1027.2) MB, 659.1 / 0.0 ms  (average mu = 0.197, current mu = 0.064) allocation failure scavenge might not succeed
[2021-07-27 23:32:30] [213:0x55d1a5f5ee40]    46819 ms: Mark-sweep (reduce) 1019.2 (1026.2) -> 1018.3 (1027.2) MB, 703.1 / 0.0 ms  (average mu = 0.127, current mu = 0.053) allocation failure scavenge might not succeed
[2021-07-27 23:32:30] 
[2021-07-27 23:32:30] 
[2021-07-27 23:32:30] <--- JS stacktrace --->
[2021-07-27 23:32:30] 
[2021-07-27 23:32:30] FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
[2021-07-27 23:32:30] error building image: error building stage: failed to execute command: waiting for process to exit: signal: aborted
[2021-07-27 23:32:30] 
[2021-07-27 23:32:30] command exited with code 1
[2021-07-27 23:32:34]  ! Build failed (exit code 1)

It works completely fine locally.

Does anyone know what could be going on here?

This is the startup script:

node --max-old-space-size=4096 build/src/index.js

Dockerfile:

FROM node:14-alpine
WORKDIR /app

COPY ./package.json ./
RUN npm install

COPY . .

ENV NODE_ENV production
RUN npm run build

CMD ["npm", "run", "start"]

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hello @luisSnorkler,

It appears you are noticing the ‘JavaScript heap out of memory’ error while building your app.

You might need to add 2 environment variables:

1.  Key:  NODE_OPTIONS
    Value: --max-old-space-size=4096

2.  Key: GENERATE_SOURCEMAP
    Value: false

The second option usually resolves the issue. Upgrading to larger sizes wouldn’t be of much help in this case.

Here’s a documentation on the steps to add environment variables.

Cheers, Taha Fatima

I am using droplet. The issue was resolved when i increased the memory size to 4GB. If you are using app platform, maybe there is a limit on the container’s virtual memory?

From running diagnostics the memory used by the build process is 1.14 GB. But even when I upgraded to the larger sizes it still failed for the same reasons.

These are the diagnostics

Files:               593
Lines:            608779
Nodes:           1855634
Identifiers:      683426
Symbols:          951989
Types:            457061
Instantiations:  1912136
Memory used:    1145909K
I/O read:          0.07s
I/O write:         0.03s
Parse time:        3.57s
Bind time:         1.12s
Check time:       12.46s
Emit time:         2.49s
Total time:       19.64s