skip to Main Content

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


  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.

    Login or Signup to reply.
  2. "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.

    Login or Signup to reply.
  3. 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); // `{ ... }`
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search