Error [ERR_REQUIRE_ESM] NodeJS CPanel

Issue

Saat sedang mendeploy website menggunakan NodeJS di cpanel, akses ke website muncul error 503. Jika dicek log error pada file stderr.log terdapat error seperti berikut.

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/user/express-web/app.js not supported.
Instead change the require of /home/user/express-web/app.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'

Server juga terinstall Cloudlinux dan Litespeed.

Resolution

Hal tersebut kemungkinan server tidak mensupport type module yang disetting pada package.json sehingga perlu dicoba dengan dynamic import app.js.

Buat file loader.

nano /home/user/express-web/loader.cjs

Lalu edit file loader seperti berikut.

async function loadApp() {
    await import("./app.js");
}

loadApp();

Atau bisa juga menggunakan kode berikut.

(() => import('./app.js'))();

Terakhir setting Application startup file yang ada di cpanel ke file loader.cjs.