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.
3
Answers
This maybe works for you:
module.parent
isnull
when yourapp.js
is called directly from node just as innode 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 scriptnode-loader.js
before yours, which becomes yourmodule.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."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):
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.
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.