ENOMEM error when starting Next.js app on shared hosting server

806 views Asked by At

TL;DR: I got an spawn ENOMEM error when trying to build and run my Next.js app on a shared hosting server, which had over 900MB RAM and 70+ processes available at the time this happened. The log showed that the RSS size was 50814976 when the error was caught.

I am not quite sure if the error is simply caused by insufficient memory or it occurs because of incorrect settings or configs. Could you please give me some advice?

==========Details below============

I am building a Next.js app with Node.js, and it’s built with a custom server (my entry point: server.js). I can run my app in a local environment on localhost:3000. Then I try deploying it to check if it’s ok on the network.

I have subscribed to A2 Hosting’s DRIVE Web Shared Hosting Plan, which is optimised to support Node.js environment. What they offer in the plan are 1GB of physical memory and 75 available processes.

My application was created through the Setup Node.js App function on the cPanel, and cloned my project into the server with git via SSH. NPM packages were also installed on the server with the command npm install. The node version was v.12.9.1 and npm version was 6.14.8.

My npm scripts were defined to run the custom server. Here were the npm scripts defined in my package.json file:

"scripts": {
    "dev": "node server.js"
    "build": "next build",
    "start": "NODE_ENV=production node server.js"
}

Then I used the command npm run build to create the production application in the .next folder, but an error spawn ENOMEM came up immediately.

I googled that it was a memory usage related issue. Some said this error occurred when memory was not enough, and the workaround to bypass this was to build the production folder locally and upload it to the server. So I copied the steps.

However, the result was frustrating when I ran the command npm run start. The error ENOMEM was still here, coming up in less than a second after I entered the command.

Cleaning the npm cache and reinstalling the npm modules didn’t seem to work either.

I tried increasing the memory limit by adding option --max-old-space-size in the command and ran NODE_ENV=production node --max-old-space-size=1024 server.js; but unfortunately this didn’t seem to work and the ENOMEM still popped up.

I added console.log(process.memoryUsage()) to show the usage when an error was caught and this was the result:

{
    rss: 50814976,
    heapTotal: 34107392,
    heapUsed: 23076064,
    external: 1632450
}

The total rss size was far less than the limit. Or did I use a wrong method to inspect the memory consumption?

How can I solve the ENOMEM problem? What exactly the error is caused by? Is it really just because the available RAM doesn't meet the requirement of a next.js app?

I am not sure if I have applied incorrect settings, overlooked some important configs, or miswritten any codes that bring about this error. I want to figure out what is going on underneath. Upgrading the plan impulsively without adequate understanding isn’t good for me as a newbie developer, and it's my responsibility to make good use of the budget.

Could you please give me some advice?

Thank you for your attention.

0

There are 0 answers