Can´t deploy aplication in cpanel
Unanswered
Standard Chinchilla posted this in #help-forum
Standard ChinchillaOP
I have trying to deploy my aplication in the server with "setup node.js" but it doesn´t work, in the documentation say that i need to add a server.js file and change something in the package.json, but i try with the next.js template and i doesn´t work
47 Replies
Standard ChinchillaOP
const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')
const dev = process.env.NODE_ENV !== 'production'
const hostname = 'localhost'
const port = 4789
// when using middleware `hostname` and `port` must be provided below
const app = next({ dev, hostname, port })
const handle = app.getRequestHandler()
app.prepare().then(() => {
createServer(async (req, res) => {
try {
const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl
if (pathname === '/a') {
await app.render(req, res, '/a', query)
} else if (pathname === '/b') {
await app.render(req, res, '/b', query)
} else {
await handle(req, res, parsedUrl)
}
} catch (err) {
console.error('Error occurred handling', req.url, err)
res.statusCode = 500
res.end('internal server error')
}
})
.once('error', (err) => {
console.error(err)
process.exit(1)
})
.listen(port, () => {
console.log(`> Ready on http://${hostname}:${port}`)
})
})this is my
server.js file. I add to in the package.json: "start": "NODE_ENV=production node server.js"Duplicate thread
https://nextjs-forum.com/post/1164368958183444560#message-1164369219413094460
You should be using
https://nextjs-forum.com/post/1164368958183444560#message-1164369219413094460
You should be using
next build and next start to start your next.js application. Custom server is for something else. https://nextjs.org/docs/app/api-reference/next-cli#productionStandard ChinchillaOP
yes... but when i run npm build, start or run, its say "RangeError: WebAssembly.instantiate(): Out of memory: wasm memory". I think i need the server.js because the "setup node.js" needs it.
Masai Lion
What's the node js version that you are currently using? Had to deploy a next js app with a custom node js express as a backend a week ago to a shared hosting provider which used cpanel. Man the horror... We had to build everything before using the file upload due to the client having 768/1240 mb of ram in usage for his other stuff. But we did that zipped everything and used a custom script to start the next js server
As I can see from the img 14.18.3 as a node js version isn't gonna help ya a lot https://nextjs.org/docs/getting-started/installation there a lot of guides out there on how to deploy next js to a cpanel I recommend you reading them and to update the node js version (manually if u can if not maybe contact the support )
Standard ChinchillaOP
Ok i try that, thanks
@Masai Lion What's the node js version that you are currently using? Had to deploy a next js app with a custom node js express as a backend a week ago to a shared hosting provider which used cpanel. Man the horror... We had to build everything before using the file upload due to the client having 768/1240 mb of ram in usage for his other stuff. But we did that zipped everything and used a custom script to start the next js server
Citrus blackfly parasitoid
Hello, I would like to answer too because I got stuck a few weeks ago, to the point where I went back to a basic React app. While trying to deploy Next on a shared hosting provider on cpanel, I get this error during the build, support tells me that the Node is saturated and they can't do anything about it..
Do you think it's related to this too?
Do you think it's related to this too?
Citrus blackfly parasitoid
And with a standalone deploy
Masai Lion
I can tell ya what we did to get it working. 1st The client app was a Crud app that had auth system, admin/user roles part , creating/editting/reading docs with maria db and a custom backend in ts express and mysql2. We had to parse a xlsx file that had like 30k rows and put it over the database so his users can query it and make docs . Its not a super hard project but we did this: 1st frontendpart(next js ) we pre build it with the env vars for the cpanel hosting (domain, api keys db conns etc matching the cpanel). 2nd we upgraded the node js version over the cpanel to 18.9.1 (we needed 18.18.x but the provider couldnt support it sadly so we had to re-do some of the xlsx files logic since const body = req.formData() was working after node js version 18.18.x). Then we run npm run build over our dev env and shipped everything with gzip(app dir .next dir(the build) all files except the .env gitignore etc and the custom server.js script). Lastly we unzipped them and made a npm install via the panel or if u have ssh you can also do it with it. To start the server we used cpanel to run the server.js script which just started an express server whom was redirecting all the inc request to the next js server. Thats it... you have to config the package.json file to have the start script pointing to the server.js file. Also we had some issues serving the static content like images but I rly dont remember how we solved that one after all the damn bshit we faced with this deployment. I wont recommend to anyone to host his next js app over a shared hosting provider specially if node js support is low and the Plan you are using is cheap( low cpu/ram).
do not zip or use the node-modules folder/files from your dev env no need to ship them over
Standard ChinchillaOP
"you have to config the package.json file to have the start script pointing to the server.js file." how i can do that?
@Masai Lion do not zip or use the node-modules folder/files from your dev env no need to ship them over
Standard ChinchillaOP
yes yes
@Citrus blackfly parasitoid Hello, I would like to answer too because I got stuck a few weeks ago, to the point where I went back to a basic React app. While trying to deploy Next on a shared hosting provider on cpanel, I get this error during the build, support tells me that the Node is saturated and they can't do anything about it..
Do you think it's related to this too?
Standard ChinchillaOP
i try with a react native aplication and it´s work
i don´t no much but when i have the index.html i don´t need server or nothing, when i use a server.js i don´t now but it blow
but my aplication is alredy made in next.js, i can´t turn it back, it´s to late, so....
how i can edit the package.json to redirect me to the server.js, i need a index.html? (to do something xd), i surprised to how hard is this....
@Standard Chinchilla js
const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')
const dev = process.env.NODE_ENV !== 'production'
const hostname = 'localhost'
const port = 4789
// when using middleware `hostname` and `port` must be provided below
const app = next({ dev, hostname, port })
const handle = app.getRequestHandler()
app.prepare().then(() => {
createServer(async (req, res) => {
try {
const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl
if (pathname === '/a') {
await app.render(req, res, '/a', query)
} else if (pathname === '/b') {
await app.render(req, res, '/b', query)
} else {
await handle(req, res, parsedUrl)
}
} catch (err) {
console.error('Error occurred handling', req.url, err)
res.statusCode = 500
res.end('internal server error')
}
})
.once('error', (err) => {
console.error(err)
process.exit(1)
})
.listen(port, () => {
console.log(`> Ready on http://${hostname}:${port}`)
})
})
this is my `server.js` file. I add to in the `package.json`:
json
"start": "NODE_ENV=production node server.js"
I'm not sure exactly what you're trying to do. Why are you using a custom server? It would be much easier to do this as a microservice
@Marchy I'm not sure exactly what you're trying to do. Why are you using a custom server? It would be much easier to do this as a microservice
Standard ChinchillaOP
sorry for the ignoranse but i don´t now how to do it
in the cpanel
@Standard Chinchilla sorry for the ignoranse but i don´t now how to do it
remove that file,
next build && next start (npm run build && npm start)Standard ChinchillaOP
askt for it
for a server.js
@Marchy ??
Standard ChinchillaOP
wait me, i show a img
@Marchy remove that file, `next build` && `next start` (npm run build && npm start)
Standard ChinchillaOP
yes... but it start in the port 3000, How can I configure it so that it is on my main domain?
@Standard Chinchilla yes... but it start in the port 3000, How can I configure it so that it is on my main domain?
That configuration happens outside of your next.js application
Standard ChinchillaOP
(it´s not my)
this is the way i doing it
@Marchy That configuration happens outside of your next.js application
Standard ChinchillaOP
how, where?
@Standard Chinchilla how, where?
Depends on the host 🤷â€â™‚ï¸ If you deploy to vercel or any other service provider that supports next (aws, azure) it handles this for you
Otherwise, you need to configure it manually based on the docs from your service provider
Standard ChinchillaOP
... it is a2 hostting
i´m using cpanel
@Marchy Otherwise, you need to configure it manually based on the docs from your service provider
Standard ChinchillaOP
yes... i try
i was able to make it work with a server.js that had only text
My knowledge goes as far as the node process in next.js, unfortunately. If you're using a non-standard deployment method, you'll need to know how to configure it correctly (and securely)
thanks for your time
i´ll keep trying
@Standard Chinchilla thanks for your time
I know it probably wasn't the answer you're looking for 😅 I'd recommend trying established patterns first if you want to move further into next.js, or more into base node.js if you want to learn there. I haven't used cPanel in a very long time since there are other free alternatives
Standard ChinchillaOP
okey... I was able to do something, i run the server.js to have the aplication in the backgrond, but how i can redirect the user to the port 3000 or how i can run the server in the domain, i mean i dont want to put example.com:3000 i want exmple.com and just that. @Marchy you say it´s depends but where/how i can find it
Masai Lion
You gonna need a reverse proxy for that nginx litespeed apache etc to make all the request that goes to your domain for example my.cooldomain.com to localhost:3000
@Standard Chinchilla "you have to config the package.json file to have the start script pointing to the server.js file." how i can do that?
Masai Lion
In the package.json file there's a script config like scripts {dev : 'next dev', build: 'next build', start: 'next start'} so you can change the start part to Node.env=production node server.js if I remember correctly
But overall going to a different provider like a vps(digital ocean), vercel, AWS will be a better if not the best thing to do in the long run for a next js app
Standard ChinchillaOP
Yes i try with vercel, it's free?
Because i deploy de aplication, a put the env variables, all works
But i don't now if it's for production