skip to Main Content

So I already deployed my website Nodejs + MySQL, but the problem is I get an error of

503 Service Unavailable The server is temporarily busy, try again later!

so I saw a thread : Instead change the require of index.js, to a dynamic import() which is available in all CommonJS modules, I followed the thread and install the

npm i [email protected]

after that, I also went back to my setup nodejs and stop the process and npm install it again and received the same error, when I check my stderr.log i get an error of this

/usr/local/lsws/fcgi-bin/lsnode.js:48
var app = require(startupFile);
^

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/ppsconl2/nodejs/index.js from /usr/local/lsws/fcgi-bin/lsnode.js not supported.
Instead change the require of index.js in /usr/local/lsws/fcgi-bin/lsnode.js to a dynamic import() which is available in all CommonJS modules.
    at startApplication (/usr/local/lsws/fcgi-bin/lsnode.js:48:15)
    at Object.<anonymous> (/usr/local/lsws/fcgi-bin/lsnode.js:16:1) {
  code: 'ERR_REQUIRE_ESM'
}

this is my package.json

{
  "name": "api",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1",
    "start": "nodemon index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "cors": "^2.8.5",
    "express": "^4.18.2",
    "mysql2": "^3.2.0",
    "nodemon": "^2.0.21"
  }
}

The hosting’s node js is v18.9.1 while in my pc is v18.14.1

2

Answers


  1. Rename your main js file (index.js) into app.mjs (maybe index.mjs works nowadays, but some cPanel documentations say the main file must be named app. Don’t forget to restart your application in cPanel Web Applications after updating the file name. I also recommend to stop the application prior to renaming the file.

    OR

    You can also update your application to use only require() instead of import

    Login or Signup to reply.
  2. So after hours of debugging i came across the solution:

    Case: Deploying nodejs App to Cpanel

    Follow these steps.

    1. Create loader.cjs in your ndoejs root directory.

    2. Import your main file dynamically into the loader.cjs: by copying the code below. Note: If your main file is not named index.js, rename it to our main file.

    async function loadApp() {
         await import('./index.js');
    }
    loadApp();
    
    1. Go to your Node App in cPanel

    2. In the "Application startup file", replace your main file with loader.cjs

    3. Save and restart your app.

    This should worked for me.

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