skip to Main Content

So I followed a tutorial on how to deploy NextJs app to a subdomain on a Cpanel hosting by adding a server.js file and modifying the Package.json file with the following:

// server.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 = process.env.port || 3000
// when using middleware `hostname` and `port` must be provided below
const app = next({ dev, hostname, port })
const handle = app.getRequestHandler() 

app.prepare().then(() => {
    createServer((req, res) => {
    // Be sure to pass `true` as the second argument to `url.parse`.
    // This tells it to parse the query portion of the URL.
    const parsedUrl = parse(req.url, true)
    const { pathname, query } = parsedUrl

    if (pathname === '/a') {
      app.render(req, res, '/a', query)
    } else if (pathname === '/b') {
      app.render(req, res, '/b', query)
    } else {
      handle(req, res, parsedUrl)
    }
    }).listen(port, (err) => {
    if (err) throw err
    console.log(`> Ready on http://${hostname}:${port}`)
    })
})


//Package.json file
...

 "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "NODE_ENV=production node server.js",
    "lint": "next lint",
    "json-server": "json-server --watch db.json --port 3004"
  }

...

I run npm build and uploaded the files to a folder that points to a subdomain. However, when I create my application in Node.js in Cpanel, the "Run NPM Install" button is greyed out and the information I keep getting is that the package.json cannot be found in the folder meanwhile it is actually there.

Node.js in Cpanel

Any help on what could be wrong or a link to a better tutorial?

2

Answers


  1. Your application root name should be the same with the application url.
    Also ensure you uploaded all your file inside your application root name.

    The components needed are the .next/ directory and files next.config.js, package.json and server.js, package-lock.json

    Login or Signup to reply.
  2. Click Stop app button and refresh page.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search