What magic are you using in the nodejs apps on require absolute paths

Hey I can call in any file in my project a require giving it something like /app/path/to/lib/ from anywhere and it just works, but on my Desktop programs it simply doesnt, What should I do to make it work like on Glitch?

So what I eman is it doesnt matter how deep I am in the working tree from where I call it it takes the /app path as the root of the project but at home it jsut doesnt

You try ./path/to/lib?

the thing is assume I have this structure

app/config.js
app/path/to/lib.js

Now I need to require config.js in app/path/to/lib.js

On Glitch I can just do require('/app/config.js') in app/path/to/lib.js and it just works, but on my desktop I have to do ../../config.js or if you want ../../../app/config.js which sucks. I want it to have the same behaviour ond ekstop as on Glitch

Best way is make something like index.js and from it require all scripts.

I want to require a PAth.js in my root from anywhere where I have all the path names of the modules. So that I can use those paths to require the other modules like so: require(Path.sources.bot) for example. But for that I need to require Path.js first and as I said on Glitch I would have just done require('/app/Path.js'). I want exactly that behaviour and they managed to get it working so Iw ant that too :slight_smile:

I mean about /root/index.js and it is in it:

const modules = 
{
"Path": require("./path/to/lib"),
"AnotherPath": require("./anotherpath/to/lib")
}
modules.Path(modules)

it should work

I solved it in another way now:

Paths and Utils will be used by every class/file in my project. init.js is my main and start file for the node project.

init.js

global.Paths = require(`${__dirname}/Paths`);
global.Utils = require(`${__dirname}/Utils`);
global.include = className => require(Paths.sources[className]);
...

Under Paths.sources are all the paths from root directory to the JS files the project will have.
So now I can use this in every class to require a Class for example the WebManager Class like so:

somefile.js

const WebManager = include('WebManager');
...

Where Webmanager would be something like this:

WebManager.js

class WebManager {
  static sendRequestTo(options, onResponseCallback) {
    Utils.request(options, onResponseCallback);
  }

  static getOptions(baseUrl, url, qs, method, json, headers) {
    return {
      baseUrl,
      url,
      qs,
      headers,
      method,
      json,
    };
  }
}

module.exports = WebManager;
1 Like