简体   繁体   中英

Custom Server for Next.js on Shared Hosting not working

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;
    ^

In the last year or so, getting Node.js projects to work on cPanel has been impossible. (I have even found the error: "Port 3000 already in use" within a cPanel account with no other application running on it. Changing port number only means a HTTP request obstruction is applied elsewhere, eg the apache2.conf file, where a shared hosting client cannot access it). Perhaps the close commercial working relationship between cPanel and WordPress may, in the immortal words of Feynman, "have some significance to our problem".

My solution is to get a cheap VPS from a big cloud hosting company for a few bucks a month, install Apache2 and other essential apps, attach a domain name to it, create a file structure for your project and amend the project for the new host URL. Then upload all the Node.js files and associated JS scripts to the Node (eg "nodeapp") and web files folders respectively in the project's VPS space.

You will have to configure Apache2 to initially allow only ports 80 and 443 open to HTTP requests. But you can add additional configs to relay all requests with "/nodeapp" in them to your established Node.js server port (3000).

Look around at the major cloud hosting companies as they may have pre-configured set-ups for your particular type of application.

But you must get away from any host - shared or otherwise - that uses cPanel or all your work will be for naught. Good luck, Buddy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM