Search code examples
node.jsreactjstypescriptmemory-managementv8

How to solve FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memor on Typescript 'npm run build'?


How to solve the: FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory 1: 0xa08900 node::Abort() [/root/.nvm/versions/node/v12.18.1/bin/node] when running npm run build on TypeScript project.

I have been trying the Fixes from my research here in Stackoverflow and other Forums but I am not winning. I updated from Node.js version 10 to version 12.18.1 but still, the problem persists. I tried to create a SWAP file but yet I didn't win. Previously the problem would go away by recreating a SWAP file. I am using Digital Ocean Ubuntu server 20.4. Please assist me to solve those issue


Solution

  • Sounds like you need to allow a larger heap size (which by default is computed automatically based on available system memory -- swap files don't count for that).

    The flag you need for that is --max-old-space-size (with a value in megabytes), apparently one way of setting it is to run npm as follows:

    node --max_old_space_size=4000 /usr/bin/npm run build
    

    or you can use NODE_OPTIONS or .npmrc if you prefer.

    If that doesn't help, or if you need more help, please provide some more information:

    • how much memory does your server have?
    • how much memory is being consumed at the point of the out-of-memory error?
    • is there any way for someone else to reproduce the problem, or does it involve proprietary software? Can you create a public repro case?

    I updated from Node.js version 10 to version 12.18.1

    Probably unrelated to your issue, but that's still a fairly old version. How about 12.20.0 (if it has to be the v12 line, which is in maintenance mode) or 14.15.1 (current "active LTS")?

    /root/.nvm/...

    Why do you run node as root??!! Don't ever run anything as root!