Sunday, 11 June 2017

NodeJS doesn't resolve path properly

I have a nodeJS server with many modules and nested modules I want to startup while console.logging a message so I made something like this.

global.REQUIRE = function(path) {
    console.log('loading module ' + path);
    return require(path);
};

And instead of having require('./constants/app-constants.js') I would have global.REQUIRE('./constants/app-constants.js');. But path isn't properly resolved because I have some nested modules and REQUIRE method is in application-main.js on root folder so require solves from there apparently. I don't think it's wrong I just comment my first attemp.

My second attempt was then using require('path').resolve and do something like this.

var resolve = require('path').resolve;
global.REQUIRE(resolve('./constants/app-constants.js'));

But this doesn't properly resolve either. Console outputs something like

/home/$USER/git/nodeJS/constants/app-constants.js

instead of what should output

/home/$USER/git/nodeJS/app/constants/app-constants.js

index.js with the requires is inside app/ folder and old require('./constants/app-constants.js') works fine from there.



via p4x

No comments:

Post a Comment