The Be Sure Blog

Code Snippets | Problem Solving | Tips & Tricks

The Be Sure Blog banner

How i finally fixed "sh: 1: next: not found"

posted on 18.5.2023 by Below Surface in "Next.js"

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.

Tags:

next.js
npm
ubuntu
github actions
linode
out of memory
swap memory

Sources:

https://github.com/vercel/next.js/issues/2756https://www.redhat.com/sysadmin/clear-swap-linux

More posts of this category

Do a static export of a Next.js website

Doing a static export in Next.js is easy and working great on basic web hosting services

Next.js

Change the port of a Next.js app

How to manually set the port from 3000 to any other number

Next.js

Next.js 13 - how to set dynamic HTML head titles

How to take a slug string and turn it into a HTML title tag

Next.js

Suppress Next.js img tag warnings

If you don't want to use their Image elements, this is how to disable the warnings

Next.js

Add a robots.txt to your Next.js website

How to add your robots.txt file

Next.js

Add Tailwind CSS to your Next.js app

A few commands and some possible error fixing and you are good to go!

Next.js

Next.js 13 Tailwind CSS hot-reload issue fix

With the currently experimental app directory, hot-reload does not work properly

Next.js

How to clear the cache of a Next.js application

How to create a fresh build without any cached data

Next.js

How I fixed "Parsing error: DeprecationError"

'originalKeywordKind' has been deprecated since v5.0.0 (...)

Next.js