6

I'm using an AWS EC2 instance with a t2.micro type and I'm trying to build my REST API that uses typescript by using the tsc command but I'm getting a memory issue.

Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

I tried to use the --max_old_space_size-1024 but it fixed nothing.

  • NodeJS: v14.9.0
  • NPM: v6.14.8

Any suggestions guys?

aingthed
  • 103
  • 2
  • 5

2 Answers2

0

I hit the same problem. My issue was that t2.micro instances only had 1GB of RAM and Node needed between 0.75 to 1 GB of RAM to boot up our server. I had to boost available memory via a swapfile then increase the memory limits for Node/V8 via --max_old_space_size.

Specifically we used a 4GB swap file and then upped Node/V8's max old space size to 2GB. Doubt those are ideal combinations / ratios but they worked for us. I imagine you could get away with a 1GB swap and 1.5GB max old space but YMMV.

Also see How do I determine the correct "max-old-space-size" for node.js? for more info and best practices

Keego
  • 3,977
  • 1
  • 16
  • 9
-2

Yesterday I also faced the same problem. Don't try to build REST API in ec2 instance. You can build it in your local and you can put build file(index.js) and package.json into ec2. Tryout this article. https://auth0.com/blog/use-typescript-to-create-a-secure-api-with-nodejs-and-express-getting-started/. Then install node.js https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/setting-up-node-on-ec2-instance.html . Then install the dependencies by "npm install" and run the build file. In my case "node index.js" make runs. Add Process manager.