I have a Next.js project and I've created a custom server for him. Everything works fine on the localhost but not works on Shared Hosting.
This is my custom server file:
const express = require('express')
const next = require('next')
require('dotenv/config')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
const port = process.env.SERVER_PORT || 3000
app.prepare().then(()=>{
const server = express()
server.get('*', (req, res) => handle(req, res))
server.listen(port, (err) => {
if(err) throw err
console.log(`Server listen on http://127.0.0.1:${port}`)
})
})
This is the package.json:
{
"name": "mywebsite-next",
"private": true,
"scripts": {
"dev": "node server.js",
"build": "next build",
"start": "node server.js",
"lint": "next lint"
},
"dependencies": {
"dotenv": "^10.0.0",
"express": "^4.17.1",
"next": "12.0.3",
"react": "17.0.2",
"react-dom": "17.0.2"
},
"devDependencies": {
"eslint": "7.32.0",
"eslint-config-next": "12.0.3"
}
}
I'm using a application to setup Node.js projects calling like "Setup Node.js Project" in the Shared Hosting with cPanel. It was setup all my Node.js projects but this time with Next.js giving error messages in the stderr.log:
Error: Cannot find module 'express'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/home/yusufcod/repositories/mywebsite-next-2/server.js:1:17)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
internal/modules/cjs/loader.js:638
throw err;
^