1

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

Joel Joseph
  • 5,889
  • 4
  • 31
  • 36
  • "seems to hit issues when developers are working / using different environment" — What issues? There shouldn't be any issues doing it the recommended way. It's the standard approach. – Quentin Jul 21 '21 at 15:11
  • @Quentin when working in windows sometimes the chances of of getting error while define 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 work better with all environment – Joel Joseph Jul 21 '21 at 15:25
  • Read your original feedback again: "There is no need to define __base and use it **to require js files**." Multer folder paths are a complete different problem. Node on Windows also has no problems whatsoever using normal module resolution paths. – Quentin Jul 21 '21 at 15:27
  • @Quentin thanks for the feedback , i was using the above method since it was working in all cases in defining folder path and also when using path while using some third party library. May be i should roll back my approach – Joel Joseph Jul 21 '21 at 15:42
  • @Quentin also this was one of the posts that i referred in the past : https://stackoverflow.com/questions/21637099/app-base-path-from-a-module-in-nodejs – Joel Joseph Jul 21 '21 at 17:29

0 Answers0