I just faced same problem with my EC2 instance t2.micro which has 1 GB memory.
I resolved the problem by creating swap file using this (https://aws.amazon.com/ko/premiumsupport/knowledge-center/ec2-memory-swap-file/) and set following environment variable.
export NODE_OPTIONS=--max_old_space_size=4096
출처)
https://stackoverflow.com/questions/38558989/node-js-heap-out-of-memory
리눅스 스왑 파티션 생성 및 삭제하기
https://willseungh0.tistory.com/13
https://aws.amazon.com/ko/premiumsupport/knowledge-center/ec2-memory-swap-file/