I recently got a feedback for one of my open Nodejs project on github and asked me to improve how i define path to import files or folder path into my Nodejs project.
Feedback i received :
There is no need to define __base and use it to require js files. Replacing "require(__base + "app/service/commands");" with "require("./app/service/commands");" works just as well.
This is how i import my files
// this is a one time declaration so that i can use `__base` every where in the project
global.__base = __dirname + '/';
// this is how i use the same in all other files
const commands = require(__base + "app/service/commands");
But from my past experience suggested method in the feedback seems to hit issues when developers are working / using different environment .
For example: In the past i used to use the suggested method, but when working in windows sometimes the chances of getting error while defining the path for files in larger projects or possibly due to human error in specifying number of dots . Also when using library like multer defining folder path etc the first methods seems to work better with all environment
Currently i am confused , so i am asking the community for more feedback. may be there's a better way . Please help me with your valuable inputs