Applicable to:
- Plesk for Linux
Symptoms
-
A Node.js application is not loading. The following error message is logged in
/var/log/passenger/passenger.log
:CONFIG_TEXT: App 266293 output: Error [ERR_REQUIRE_ESM]: require() of ES Module /var/www/vhosts/wachatbot.org/httpdocs/app.js from /usr/share/passenger/helper-scripts/node-loader.js not supported.
App 266293 output: Instead change the require of app.js in /usr/share/passenger/helper-scripts/node-loader.js to a dynamic import() which is available in all CommonJS modules.
App 266293 output: at Module.require (/usr/share/passenger/helper-scripts/node-loader.js:80:25)
App 266293 output: at loadApplication (/usr/share/passenger/helper-scripts/node-loader.js:243:2)
App 266293 output: at setupEnvironment (/usr/share/passenger/helper-scripts/node-loader.js:214:2)
App 266293 output: at Object.<anonymous> (/usr/share/passenger/helper-scripts/node-loader.js:133:1) {
App 266293 output: code: 'ERR_REQUIRE_ESM'
App 266293 output: } -
Node.js application startup file has import statements:
CONFIG_TEXT: import React from "react";
import Link from "next/link";
Cause
According to this Phusion Passenger thread in GitHub, new ESM syntax cannot be used to import modules in Node.js:
Currently, we can't use the new ESM syntax to import modules in Node.js
That's because, inside the loadApplication function in src/helper-scripts/node-loader.js, we use require to start the application, instead, we should use childProcess.fork so we can support both CommonJS and ESM imports not only CommonJS as it is currently the case, it should not break existing applications and older Node.js versions, as you can see in the documentation, it is supported since Node.js v0.5.0 (see: https://nodejs.org/dist/latest-v16.x/docs/api/child_process.html#child_process_child_process_fork_modulepath_args_options).
Resolution
Apply the workaround suggested by the Phusion Passenger github page:
1. Create a new application startup file _passenger.cjs
in the application home directory.
2. Open it and add the following content:
CONFIG_TEXT: async function main () {
await import('./app.js')
}
main()
where app.js
is the application startup file.
3. Go to Domains > example.com > Node.js and change Application Startup File to _passenger.cjs
.
Comments
1 comment
Does this still apply today? as of 09-2024?
Please sign in to leave a comment.