El domingo, 13 de octubre de 2013 18:11:21 UTC+2, Michael Schoonmaker escribió: > > We've had this conversation before, and we don't need to have it again. > That said and for posterity, I'd like folks to know what tools they have > available to them. > > If you're writing a shell script and want to operate on the user's files > (e.g. `myscript dir/file.txt`, use `process.cwd()`. That's both what it's > for and what the user will typically expect. >
Correct, the cwd issue is only related when you need to load a file relative to the root (the main file). > If you're operating on a file packaged into your module and want a path > relative to the currently-running file rather than the user's current > working directory, use `__dirname`. That's what _it's_ for. > Agree. > If you're operating on a file packaged into your module and you _really_ > want to build that path relative to the file that was originally run (e.g. > `node FILE`), use the longer `path.dirname(require.main.filename)`. That > will give you the directory containing the file node was run against, and > you can go from there. > Agree, but you still have to prefix the path: https://github.com/gagle/node-rwd#alternatives > > One final note: `require` needs none of this madness. It's always relative > to the file doing the `require`-ing unless it's an absolute path like > `/home/me/my/stuff.json`. > Agree. > > - Schoon > > > On Sun, Oct 13, 2013 at 6:34 AM, Gagle <[email protected] <javascript:>>wrote: > >> You can easily forget to prefix a path with __dirname, so months ago (in >> fact, years ago) I began implementing a solution to this issue because I >> don't like to write the magic __dirname variable in front of all the paths. >> >> This issue is related with the OS, not the programming language. There >> are multiple solution like changing silently the cwd at runtime but this is >> not the best way (as I was told). IMO the best way is to inform the user >> and exit the process, so if the process doesn't exit then you can safely >> use any path without __dirname. >> >> Simply put this line in the top of your main file: require("rwd") >> >> https://github.com/gagle/node-rwd >> >> -- >> -- >> 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.
