cPanel NodeJs | ERR_REQUIRE_ESM

If you are using cPanel > Setup Node.Js App and you have across of a similar error to:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: app.js

You have to know that you are using ECMAScript (ESM) and that there is an import incompatibility.

Your Startup file would probably work fine if run on cPanel’s terminal, but not on the web. This is a very commune issue, especially with a number of recent frameworks.

Read more: https://adamcoster.com/blog/commonjs-and-esm-importexport-compatibility-examples


The cause is that cPanel’s CloudLinux Selector uses as reverse proxy Phusion Passenger (the software in charge of redirecting your node app from its port to the default HTTP(S) ports). This reverse proxy takes as a parameter a Startup file (e.g.: server.js, app.js, index.js), if this file isn’t following the CJS (CommonJS norm), it wouldn’t work for you. Therefore you are using a framework, you have to use the CommonJS version of it, if available; or you try to reedit it and wrap the ESM.

https://docs.cloudlinux.com/cloudlinux_os_components/#limitations

Example of error message suggesting to change require to import():

Error [ERR_REQUIRE_ESM]: require() of ES Module /path/server.js from /usr/local/lsws/fcgi-bin/lsnode.js not supported.
Instead change the require of server.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'
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *