If this is an AWS instance and you are running a t2.micro / t3.micro (1GB RAM), then you can momentarily change the instance to a t2.small for 2GB. If it fails again, do t2.medium to get 4GB of RAM. Then run npm install. Once finished, change it back to t2.micro.
It will only cost a couple cents.