A high-level follow-up: it seems like something is getting lost based on what I've read of the other responses, so let's not lose the forest through the trees.
If your app only uses the front-end portion of nextjs, it should be fine on shared hosting whether the host provides nodejs or not, as the build command makes it deployable on any server that can serve HTML. The JS is then compiled on each client.
If you used nextjs's backend server for your app, that's when you need a compatible version of nodejs running on your host. If you're not using them, it should be fine, because it's basically just like React.
I have deployed several React (CRA) apps to my shared host after build and they all work fine. The host is an aged cheap shared host running cloudlinux 6 (think: Linux kernel ~3.10) and does not provide nodejs backend, only Apache web server.
If they use the backend server (e.g. nextjs's ,app.js
), I deploy my nextjs apps to my VPS hosted by HyperExpert - they provide a blank slate OS (mine's Ubuntu 20.04 ATM). It requires you to set up your own proxy server (e.g. nginx) and web server (e.g. lighthttpd, Apache, or nginx can do both) but you have much more flexibility as to what you decide to use.
On a VPS, you can also package your apps as containers via docker, podman, systemd-container, etc. which can help make re-deployment cleaner in the case of updates, and aid in scheduling, such as running your apps as a service and limiting resources.
Important: this is where I think people are getting lost
In either case, you always need to run the build command LOCALLY to prepare your app for deployment.
Then all you need for your server is to upload the resulting folder (e.g. .next
) to your host. Create its own folder for it, and you can open mydomain.com/folderyouuploaded
in your browser.
Tip: If you name it something without a .
in the beginning, it won't be hidden. E.g. run mv .next myappnamev1.0.0
before uploading it.
Most people will also direct via your domain using a sub-domain (myapp.mydomain.com
) that points to the folder containing index.html
in your built folder you uploaded. This is usually done in cPanel, or whatever your shared host's panel type is.
You should never have to run npm i
unless you want to run your app in developer mode (I can't think of a good reason to do this on a host).
If you want to containerize it, it is a bit more involved, but it pays off in the end. With tools like toolbox or if you use a tar.gz
systemd-container, you can even edit files inside the container while they're in place, which can certainly be helpful in certain scenarios (think: little tweaks where you'd prefer to avoid an entire re-deployment of your container).
If your app grows large enough, I'd recommend a CI/CD pipeline, but that's beyond the scope of this answer. Good luck!
app.js
), it only needs a web server to run. The JS will be compiled either during the build process, or in the client browser. – Weekly