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 require()ing.

I don't see what "problem" there is here.

> The cwd has nothing to do with any language because it is
> part of the os but it can break your app and it's nearly
> impossible to fix it if you don't know that cwd != dir of main file.

But why would you think that the cwd is ever guaranteed to be the dir
of the main file?

The working directory is a well established concept in computer
programming that every platform exposes in some way.  It's "the dir
that you're in when you run the program", like what the `getcwd()`
system call returns.

If you want to know the main module's filename, you can look at
`require.main.filename`.

Can you point to a real world use case where this causes a problem?

On Wed, Aug 28, 2013 at 6:01 PM, Nathan Rajlich <[email protected]> wrote:
> 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 using process.cwd() or
> invoking node with a dir path on the filename.
>
>
> On Mon, Aug 26, 2013 at 3:08 PM, mks <[email protected]> wrote:
>>
>> 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 <[email protected]> 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 <[email protected]> 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 <[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
>>>>>>>>> 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.
>>>>>>>>
>>>>>>>>
>>>>>>> --
>>>>>>> --
>>>>>>> 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.
>>>>>>
>>>>>>
>>>>> --
>>>>> --
>>>>> 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.
>>>>
>>>>
>> --
>> --
>> 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.
>
>
> --
> --
> 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.

-- 
-- 
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.

Reply via email to