• Please be aware: Kaspersky Anti-Virus has been deprecated
    With the upgrade to Plesk Obsidian 18.0.64, "Kaspersky Anti-Virus for Servers" will be automatically removed from the servers it is installed on. We recommend that you migrate to Sophos Anti-Virus for Servers.
  • The Horde webmail has been deprecated. Its complete removal is scheduled for April 2025. For details and recommended actions, see the Feature and Deprecation Plan.
  • We’re working on enhancing the Monitoring feature in Plesk, and we could really use your expertise! If you’re open to sharing your experiences with server and website monitoring or providing feedback, we’d love to have a one-hour online meeting with you.

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