Upfront: This error occurred because my web server was out of memory during the process of NPM install of the Next.js client. It's very likely that you have another problem. Please see the below provided link for other troubleshooting ideas. But maybe, if they don't help, maybe you really face the same issue as i did!
I run an Ubuntu server with Nginx as a reverse proxy and multiple websites with Node.js backends and Next.js frontends. It's all hosted on a Linode Nanode, the smallest tier hosting package. The Node processes are managed with PM2 and I update the websites via GitHub actions. So a commit to a git branch triggers a
git pull
on the server, where then the NPM packages are then installed and the PM2 processes updated to the new website version.
Interestingly the GitHub action would not fail, but the website would be down nevertheless. In the GitHub action logs, I could see that the command
npm run build
would fail with the status code
sh: 1: next: not found
The usual research on Google and also ChatGPT would not solve my problem, even if they were pointing me in the correct direction.
Solution: The build would fail, because the
npm install
did not succeed. But why? The Linode dashboard did not show anything suspicious. Maybe a Linux issue? Because on my local development machine, the install and build run without any flaw (Windows). So I spun up a virtual machine with Kali Linux running and there I could clone, install and build the website. So i figured, there must be a problem with the web servers resources!
By running
top
I could see the running processes and resource usage of the machine. And I could see that the swap memory usage was at around 99%. Running the command
htop
would visualize this issue even better. The server had run out of swap memory and this was the reason why the NPM install failed, and this led the build to fail.
So I restarted the machine and stopped all PM2 processes. The resources looked good now. So, one by one i restarted the PM2 processes again and also ran a bash script that would update one website. And it finally worked again!
How to extend the swap memory:
https://besure.blog/how-to-increase-the-swap-memory-size-in-ubuntu
Some helpful commands:
top // or htop
For monitoring the servers resources.
free -m // check free space sudo swapoff -a // turn off swap (will take some time, you can see it happen in htop!) sudo swapon -a // turn on swap again
To try turning off and on again swap.