2

I am working on an existing project and found this piece of code in my app.js

//if (!module.parent) {
  // Server listen
  app.listen(mysettings.port, function () {
    console.log(`Server ready at ${ENV}:${mysettings.port}`);
  });
//}

When I run locally, app.listen hits and my project runs, when I upload my project on a ubuntu server, the project does not run, but times out. When I comment out the condition on ubuntu the project runs perfectly. I know on my localhost, module.parent is null.

Any help would be apperciated.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
user979331
  • 11,039
  • 73
  • 223
  • 418
  • 1
    What do you mean by "*upload my project on a ubuntu server*"? How is your script executed by that server? And how do you execute it locally? – Bergi Dec 06 '21 at 02:33
  • 1
    Do you understand what the condition does? And is it even necessary in your project? – Bergi Dec 06 '21 at 02:34
  • @Bergi I have no idea what the condition does or if it is necessary to the project. Locally, I would use npm i, then npm start...on this ubuntu server, we are using something called Plesk (first time using it) basically you upload your project and then in the Plesk dashboard there is an option to enable nodejs and you tell it where the path is and what the file is called, then are options to restart app, npm install, run a scrip (using build) and it runs. – user979331 Dec 06 '21 at 03:39
  • For the purpose of the condition, see [here](https://stackoverflow.com/q/6398196/1048572). Maybe ask the developer who originally added it? As for plesk, I have no idea. – Bergi Dec 06 '21 at 08:58
  • #1 Did you solve your problem? #2 If your current team don't have knowledge about plesk **AND** your app is classic nodejs (web or api), use docker instead plesk.I can help you . #3 Could you verify if plesk is the problem? – JRichardsz Dec 10 '21 at 19:58
  • 1
    Can you do a `console.log(module.parent)` and post here the output? I am wondering what it looks like. – Ionică Bizău Dec 11 '21 at 19:26
  • @user979331 the ubuntu server is most likely using `require` to get your scripts.. meaning that the app won't run, meaning that it will time out.. may I ask WHY you have that condition? what would you do if your script has a parent? – The Bomb Squad Dec 12 '21 at 02:33

3 Answers3

1

This maybe works for you:

if ((!module.parent) || (("exports" in module.parent) && ("PhusionPassenger" in module.parent.exports))) {
  // Server listen
  app.listen(mysettings.port, function () {
    console.log(`Server ready at ${ENV}:${mysettings.port}`);
  });
}

module.parent is null when your app.js is called directly from node just as in node app.js.

I believe Plesk utilizes PhusionPassenger under the hood (at least Plesk official node.js extension which is probably your case). Being this the case, your app is probably called with the command passenger start --app-type node --startup-file app.js which pre-loads a script node-loader.js before yours, which becomes your module.parent.

If you want the same behaviour for both direct node and Passenger's call, then you have to replace every (!module.parent) filter in your code with (!module.parent) || (("exports" in module.parent) && ("PhusionPassenger" in module.parent.exports)). By doing this you will keep avoiding the situations the original developer probably intended to avoid.

brunoff
  • 4,161
  • 9
  • 10
0

"times out" generally means that it cannot bind a port.

Likely you'd have to fix the environment, in order to access the desired ENV values.
Try setting the environment globally (requires a reboot before this is being picked up):

sudo nano /etc/environment

Also make sure that mysettings.port is even accessible / not in use by something else.
This answer appears related, so there may even be two ways how to define the environment.
(my answer is rather the Linux version - and the linked answer seems to be the Plesk version). When loading from file, there still is the chance that one has suitable values - and the other one doesn't.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
0

module.parent is a special property that's set by Node when you require() a module. It's a reference to the module that required this one. If you're the top-level module, it's null. If you're a child module, it's the parent module. If you're a grandchild, it's the grandparent. And so on.

// $ node app.js
console.log(module.parent); // `null`

// require('./app')
console.log(module.parent); // `{ ... }`
Rahul Sharma
  • 9,534
  • 1
  • 15
  • 37