Kofi Talent
Kofi Talent

Reputation: 195

Deploy Nextjs App to Cpanel Subdomain Not Working

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?

Upvotes: 1

Views: 1555

Answers (3)

Eyuael Bezabeh
Eyuael Bezabeh

Reputation: 81

enter image description here

and be sure to replace this line from the file "\node_modules\next\dist\build\handle-externals.js".

From: return commonjs ${localRes.replace(/.*?next[/\\]dist/, "next/dist")}

To: return commonjs ${localRes.replace(/.*?next[/\\]dist/, "next/dist").replace(/\\/g, "/")}

Around Line 178 /179

if it exists

Upvotes: 0

Krypker
Krypker

Reputation: 1

Click Stop app button and refresh page.

Upvotes: 0

Opeyemiademon
Opeyemiademon

Reputation: 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

Upvotes: -1

Related Questions