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 <[email protected] > <javascript:>>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.jsonbut 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 <[email protected]> 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 [email protected] >>>> >>>> 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 [email protected]<javascript:> >> To unsubscribe from this group, send email to >> [email protected] <javascript:> >> 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 [email protected] <javascript:>. >> 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 [email protected] To unsubscribe from this group, send email to [email protected] 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 [email protected]. For more options, visit https://groups.google.com/groups/opt_out.
