./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 global variable that tells you which is your app path. Or
even pass it to the required file. You have several options, but we still
don't know what problem you are trying to solve.
On Mon, Aug 26, 2013 at 10:42 AM, Angel Java Lopez <[email protected]>wrote:
> 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 <[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<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/**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 [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]
>> 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.