• Our team is looking to connect with folks who use email services provided by Plesk, or a premium service. If you'd like to be part of the discovery process and share your experiences, we invite you to complete this short screening survey. If your responses match the persona we are looking for, you'll receive a link to schedule a call at your convenience. We look forward to hearing from you!
  • We are looking for U.S.-based freelancer or agency working with SEO or WordPress for a quick 30-min interviews to gather feedback on XOVI, a successful German SEO tool we’re looking to launch in the U.S.
    If you qualify and participate, you’ll receive a $30 Amazon gift card as a thank-you. Please apply here. Thanks for helping shape a better SEO product for agencies!
  • The BIND DNS server has already been deprecated and removed from Plesk for Windows.
    If a Plesk for Windows server is still using BIND, the upgrade to Plesk Obsidian 18.0.70 will be unavailable until the administrator switches the DNS server to Microsoft DNS. We strongly recommend transitioning to Microsoft DNS within the next 6 weeks, before the Plesk 18.0.70 release.
  • The Horde component is removed from Plesk Installer. We recommend switching to another webmail software supported in Plesk.

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