skip to Main Content

When deploying nextjs app to c-panel hosting it asks for entry point of the application which default is app.js. In normal react application it’s totally in control but when using nextjs it’s not clear which js file is resposible to fire-up the application.

enter image description here

any idea on picking right js file as application entry point?

EDIT:

My hosting provider provided me with following code to setup an express app (which uses next’s request handler) to handle the request:

const express = require('express');
const next = require('next');

const dev = process.env.NODE_ENV !== 'production';
const nextApp = next({ dev });
const handle = nextApp.getRequestHandler();

const port = 3454;

nextApp.prepare().then(() => {
  const app = express();

  app.get('*', (req, res) => {
    return handle(req, res);
  });

  app.listen(port, err => {
    if (err) throw err;
    console.log(`> Ready on localhost:${port}`);
  });
});

it works but it’s slow because it compiles source files on demand to server requests.

3

Answers


  1. Chosen as BEST ANSWER

    The best solution for me was using some process manager to bootstrap the application.So i used PM2 to start the application and keep it running.I issued following command in a c-panel terminal and now things work well :

    NODE_ENV=production pm2 start server.js
    

    given server.js has following code :

    const express = require('express');
    const next = require('next');
    
    const dev = process.env.NODE_ENV !== 'production';
    const nextApp = next({ dev });
    const handle = nextApp.getRequestHandler();
    
    const port = 3454;
    
    nextApp.prepare().then(() => {
      const app = express();
    
      app.get('*', (req, res) => {
        return handle(req, res);
      });
    
      app.listen(port, err => {
        if (err) throw err;
        console.log(`> Ready on localhost:${port}`);
      });
    });
    

  2. I’m surprised to see that cpanel have a feature to start up nodejs application.

    what you need to understand about app.js:

    App.js contains a web server application (from the code above, your hosting provider suggested you to use ExpressJS – the most being used JS web server application) to serve web files to the browser (similar to Apache).

    “it works but it’s slow because it compiles source files on demand to
    server requests.”

    Do you have package.json file?

    Do you know what command cpanel has ran to start your application?

    Please check if your NextJS App run on development or production mode.

    Login or Signup to reply.
  3. You just need to export your nextjs application, it would work with

    pageswhether there are pages

    • index.js
    • example.js

    or

    app.jswhether there is an app.js file

    just add following scripts

     "scripts": {
        "build": "next build",
        "export": "next export",
        "serve": "serve out"
      },
    

    You can first build your project and then export it. then you can serve it to check how would it deploy .

    Incase of cPanel just extract the nextjs build folder probably named outto your folder like xyz.com.

    There would be an index.html in the build that is your main file.

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