Re: [nodejs] Re: The problem with the current working directory

2013-08-28 Thread Nathan Rajlich
The cwd is a concept that many CLI programs leverage to their advantage, namely npm and node-gyp, which execute their actions aginst the cwd. That said, it's a powerful tool, and not at all a problem, as stated pretty well in this thread already. There won't be any warning being added to node for

Re: [nodejs] Re: The problem with the current working directory

2013-08-28 Thread Isaac Schlueter
This is all working as intended. process.cwd() returns the current working directory of the process. Anything else would be surprising and wrong. __dirname and __filename are local to the module (ie, like __FILE__ in C programs). require() resolves relative links relative to the file doing the

[nodejs] Re: The problem with the current working directory

2013-08-26 Thread mks
__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 You received this message because you are subscribed to the Google Groups nodejs group. To post to this group,

[nodejs] Re: The problem with the current working directory

2013-08-26 Thread Gagle
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:

Re: [nodejs] Re: The problem with the current working directory

2013-08-26 Thread Angel Java Lopez
Gagle, can you write down a concrete use case? On Mon, Aug 26, 2013 at 9:59 AM, Gagle gagle...@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. --

Re: [nodejs] Re: The problem with the current working directory

2013-08-26 Thread Martín Ciparelli
Well then it's your problem to know where your local file is located in relation to your main file On Mon, Aug 26, 2013 at 9:59 AM, Gagle gagle...@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ó:

[nodejs] Re: The problem with the current working directory

2013-08-26 Thread Jeroen Janssen
From http://nodejs.org/docs/latest/api/globals.html#globals_dirname it states: __dirname isn't actually a global but rather local to each module. So it actually works in each module (not just the main file). Best regards, Jeroen Janssen Op maandag 26 augustus 2013 14:25:26 UTC+2 schreef

Re: [nodejs] Re: The problem with the current working directory

2013-08-26 Thread Gagle
$ 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!!

Re: [nodejs] Re: The problem with the current working directory

2013-08-26 Thread Angel Java Lopez
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 gagle...@gmail.com wrote: $ pwd /home/user1 $ mkdir dir $ cat dir/app.js console.log (process.cwd

Re: [nodejs] Re: The problem with the current working directory

2013-08-26 Thread Gagle
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

Re: [nodejs] Re: The problem with the current working directory

2013-08-26 Thread Angel Java Lopez
Ok, only to be aligned, can you write down a concrete use case? Now you switched to a require issue, not fs. On Mon, Aug 26, 2013 at 10:34 AM, Gagle gagle...@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

Re: [nodejs] Re: The problem with the current working directory

2013-08-26 Thread Martín Ciparelli
./a.js require('./b_dir/b'); ./b_dir/b.js var fs = require('fs'); console.log(fs.readFileSync(__dirname + '/../a.js', 'utf-8')); As I was saying, it's your problem to know where your required file is located relative to the main file in your application. Or you could always use some sort of

Re: [nodejs] Re: The problem with the current working directory

2013-08-26 Thread Tim Caswell
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

Re: [nodejs] Re: The problem with the current working directory

2013-08-26 Thread Angel Java Lopez
+100 Relative requires combined with npm dependencies are the next big thing since sliced bread ;-) On Mon, Aug 26, 2013 at 12:05 PM, Tim Caswell t...@creationix.com wrote: Node's require is always relative to the file that calls require. In fact, the internal implementation of this is done

Re: [nodejs] Re: The problem with the current working directory

2013-08-26 Thread Gagle
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

Re: [nodejs] Re: The problem with the current working directory

2013-08-26 Thread Martin Cooper
On Mon, Aug 26, 2013 at 9:10 AM, Gagle gagle...@gmail.com 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? Because it's not a problem, it's a useful feature,

Re: [nodejs] Re: The problem with the current working directory

2013-08-26 Thread mks
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