The point is that cwd is not problematic at all. Think of a command line program that you want to do something with your current working directory:
$ /usr/local/bin/myls $ cd ~ $ myls On Monday, August 26, 2013 6:10:07 PM UTC+2, Gagle wrote: > > If node.js is better at requiring modules than other platforms why not > include a warning when you execute a file with a relative path different > than the directory of this file? The "process.root" solution is already > implemented: > > process.root = path.dirname(process.mainModule.filename) > > We could say: "Hey! Node.js is aware the cwd problematic! Awesome" > > El lunes, 26 de agosto de 2013 17:05:16 UTC+2, Tim Caswell escribió: >> >> Node's require is always relative to the file that calls require. In >> fact, the internal implementation of this is done by giving file a unique >> copy of the require function that embeds that file's directory. If you >> wanted to require relative to the cwd, then use process.cwd and >> path.resolve. If you want to require relative to your main module, store a >> value somewhere in your process (process.root maybe?) and use that in >> conjunction with path.resolve. >> >> The fs operations are relative to the process.cwd instead of the calling >> file. If you want them to be relative to the calling file, simply >> path.resolve with __dirname and they will act like require does. >> >> Noce gives you more options in this regard than any other platform I've >> worked with. After working with node's require semantics, it's now painful >> for me to use other module systems because I can't write portable libraries >> as easily. Sometimes it's not even possible to have a set of >> inter-dependencies that only use relative requires. >> >> >> On Mon, Aug 26, 2013 at 8:34 AM, Gagle <gagl...@gmail.com> wrote: >> >>> But now suppose you have another file called b.js that is required by >>> a.js and is stored in a different directory and uses a relative path. You >>> can't use __dirname. >>> >>> El lunes, 26 de agosto de 2013 15:32:17 UTC+2, ajlopez escribió: >>>> >>>> Ah! I see.. >>>> >>>> But this is not a problem for require, but for fs. >>>> >>>> Ok, in my code I would use >>>> >>>> if (fs.existsSync(path.join(__**dirname, "settings.json")) >>>> >>>> >>>> On Mon, Aug 26, 2013 at 10:27 AM, Gagle <gagl...@gmail.com> wrote: >>>> >>>>> $ pwd >>>>> /home/user1 >>>>> $ mkdir dir >>>>> $ cat > dir/app.js >>>>> console.log (process.cwd ()); >>>>> $ node dir/app.js >>>>> /home/user1 >>>>> $ cd dir && node app.js >>>>> /home/user1/dir >>>>> >>>>> Now suppose you have this code: >>>>> >>>>> //app.js >>>>> var fs = require ("fs"); >>>>> if (fs.existsSync ("settings.json")){ >>>>> doSomethingUseful (); >>>>> }else{ >>>>> //Warning!! >>>>> saveToDatabaseDefaultSettings (); >>>>> } >>>>> >>>>> And execute the main file with a relative path: >>>>> >>>>> node dir/app >>>>> >>>>> You will enter into the else case because settings.json is stored >>>>> inside dir but the cwd is not dir, so you expect the path >>>>> ./dir/settings.json but in fact the path is ./settings.json. >>>>> >>>>> El lunes, 26 de agosto de 2013 15:05:10 UTC+2, ajlopez escribió: >>>>>> >>>>>> Gagle, can you write down a concrete use case? >>>>>> >>>>>> >>>>>> On Mon, Aug 26, 2013 at 9:59 AM, Gagle <gagl...@gmail.com> wrote: >>>>>> >>>>>>> Because it's local to the file, it doesn't work anywhere. >>>>>>> >>>>>>> El lunes, 26 de agosto de 2013 14:46:55 UTC+2, mks escribió: >>>>>>> >>>>>>>> __dirname is local to any file. It works everywhere. >>>>>>>> >>>>>>> -- >>>>>>> -- >>>>>>> Job Board: http://jobs.nodejs.org/ >>>>>>> Posting guidelines: https://github.com/joyent/**node** >>>>>>> /wiki/Mailing-List-**Posting-**Guidelines<https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines> >>>>>>> You received this message because you are subscribed to the Google >>>>>>> Groups "nodejs" group. >>>>>>> To post to this group, send email to nod...@googlegroups.com >>>>>>> >>>>>>> To unsubscribe from this group, send email to >>>>>>> nodejs+un...@**googlegroups.com >>>>>>> >>>>>>> For more options, visit this group at >>>>>>> http://groups.google.com/**group**/nodejs?hl=en?hl=en<http://groups.google.com/group/nodejs?hl=en?hl=en> >>>>>>> >>>>>>> --- >>>>>>> You received this message because you are subscribed to the Google >>>>>>> Groups "nodejs" group. >>>>>>> To unsubscribe from this group and stop receiving emails from it, >>>>>>> send an email to nodejs+un...@**googlegroups.com. >>>>>>> >>>>>>> For more options, visit >>>>>>> https://groups.google.com/**grou**ps/opt_out<https://groups.google.com/groups/opt_out> >>>>>>> . >>>>>>> >>>>>> >>>>>> -- >>>>> -- >>>>> Job Board: http://jobs.nodejs.org/ >>>>> Posting guidelines: https://github.com/joyent/** >>>>> node/wiki/Mailing-List-**Posting-Guidelines<https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines> >>>>> You received this message because you are subscribed to the Google >>>>> Groups "nodejs" group. >>>>> To post to this group, send email to nod...@googlegroups.com >>>>> To unsubscribe from this group, send email to >>>>> nodejs+un...@**googlegroups.com >>>>> For more options, visit this group at >>>>> http://groups.google.com/**group/nodejs?hl=en?hl=en<http://groups.google.com/group/nodejs?hl=en?hl=en> >>>>> >>>>> --- >>>>> You received this message because you are subscribed to the Google >>>>> Groups "nodejs" group. >>>>> To unsubscribe from this group and stop receiving emails from it, send >>>>> an email to nodejs+un...@**googlegroups.com. >>>>> For more options, visit >>>>> https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out> >>>>> . >>>>> >>>> >>>> -- >>> -- >>> Job Board: http://jobs.nodejs.org/ >>> Posting guidelines: >>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines >>> You received this message because you are subscribed to the Google >>> Groups "nodejs" group. >>> To post to this group, send email to nod...@googlegroups.com >>> To unsubscribe from this group, send email to >>> nodejs+un...@googlegroups.com >>> For more options, visit this group at >>> http://groups.google.com/group/nodejs?hl=en?hl=en >>> >>> --- >>> You received this message because you are subscribed to the Google >>> Groups "nodejs" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to nodejs+un...@googlegroups.com. >>> For more options, visit https://groups.google.com/groups/opt_out. >>> >> >> -- -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en --- You received this message because you are subscribed to the Google Groups "nodejs" group. To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.