Skip to main content
  1. Posts/

Error [ERR_REQUIRE_ESM] NodeJS CPanel

1 min
nodejs cpanel nodejs
Table of Contents

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.

Related

Command 'ng' not found pada Angular
1 min
nodejs angular nodejs
Fix Knex: run $ npm install mysql2 --save EEXIST: file already exists
1 min
nodejs nodejs
Web Scraping with Node.js
2 mins
nodejs nodejs
Build WhatsApp Bot with whatsapp-web.js
4 mins
nodejs nodejs
Installing Nodejs
1 min
nodejs linux nodejs
Serve HTML using Express.js
1 min
nodejs html nodejs