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
Rename your main js file (
index.js
) intoapp.mjs
(maybeindex.mjs
works nowadays, but some cPanel documentations say the main file must be namedapp
. 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 ofimport
So after hours of debugging i came across the solution:
Case: Deploying nodejs App to Cpanel
Follow these steps.
Create loader.cjs in your ndoejs root directory.
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.
Go to your Node App in cPanel
In the "Application startup file", replace your main file with loader.cjs
Save and restart your app.
This should worked for me.