• If you are still using CentOS 7.9, it's time to convert to Alma 8 with the free centos2alma tool by Plesk or Plesk Migrator. Please let us know your experiences or concerns in this thread:
    CentOS2Alma discussion

Question NodeJS does not work with type module

Riculum

New Pleskian
I am trying to get a small webserive working with Express. My original code was throwing errors so I checked to see what could be wrong. I found out when I import things, it doesn't work. For example:
import express from "express"
const app = express()

app.get('/', function (req, res) {
res.send('Hello World')
})

app.listen(80, async () => {
console.log(`Server ready at http://localhost:${80}`)
})

The use of require will work:
const express = require("express")
const app = express()

app.get('/', function (req, res) {
res.send('Hello World')
})

app.listen(80, async () => {
console.log(`Server ready at http://localhost:${80}`)
})

And yes, I have set "type": "module" in package.json and yes, I have restarted the server.

Could you possibly see where the problem might be?
 
Sitting now for a few days to solve the problem. I'm beginning to suspect that it's the way Phusion Passenger® works. When I run the app on my local computer or via RaspberryPi, it runs without any problems. I suspect it is because Phusion Passenger does not support imports.
Has anyone had the same experience?
 
Hi friend,

I also had this issue and solved it by wrapping the module file in a commonJS file, and using that as the entrypoint for my app.

For example lets say your main file is index.js, then create a file called index.cjs and do this:

import('./index.js').then(() => console.log('@@@@@@@ IT WORKED')).catch(e => console.log('@@@@@@@ IT FAILED', e.message, e.stack));

and then in plesk in the nodejs extension for your app make sure the main executable is index.cjs instead of index.js. Now the commonjs file is runnable via plesk, and it will load index.js as a module.

I did this to run vite in plesk to serve my node app.
 
Danke für den Tipp. Funktioniert auch soweit. Jedoch wenn ich in der index.js das Paket "import 'dotenv/config'" importieren möchte, funktioniert das Ganze nicht mehr :rolleyes:. Zuvor wurde express importiert, das hat geklappt...
 
Ist natürlich absolut lästig. Ich hatte vor einiger Zeit einen Chat mit dem Support, die meinten dass die Nutzung von ES6-Syntax oftmals buggy ist und eigentlich nur mit require() alles funktioniert.
Hier gibt es übrigens eine Idee, dass Plesk auf die neuste Passenger-Version updaten soll...
 
Back
Top