Home Error [ERR_REQUIRE_ESM] NodeJS CPanel
Post
Cancel

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

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

Lalu edit file loader seperti berikut

1
2
3
4
5
async function loadApp() {
    await import("./app.js");
}

loadApp();

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

This post is licensed under CC BY 4.0 by the author.