Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Almost any function (except for <code>require</code>) which takes file paths as an argument, will at one point use functions from the <code>fs</code> module to read from or write to them. </p> <p><a href="http://nodejs.org/api/fs.html">Node.js documentation for fs module</a> says:</p> <blockquote> <p>Relative path to filename can be used, remember however that this path will be relative to process.cwd().</p> </blockquote> <p>When you think about it, it would require major trickery to have these functions behave any differently. After all, the fs functions are regular Javascript and they don't have special access to information about the caller. The only <code>__dirname</code> they could access would be the <code>__dirname</code> of their own module (the core fs module).</p> <p>The fact that the <code>require</code> function <em>can</em> resolve paths relative to the current <code>__dirname</code>, without specifying this explicitly, is because <code>require</code> is a unique function for every single file it appears in. This way, it has access to the current module's specifics, and in particular its path.</p> <p>The reason your code happens to work is that currently, the <code>app.js</code> (or similar) the code above appears in happens to be in the same directory as what <code>process.cwd()</code> currently is. I.e. starting the app with <code>node app.js</code> would work, while starting the app with <code>node myappdir/app.js</code> (ran from its parent directory) would not. <code>process.cwd()</code> would be different.</p> <p>As long as you keep in mind that relative paths will be resolved via <code>process.cwd()</code>, then you could use the shorter syntax. In some cases it can be an advantage. It does make your code dependent on how it's called though. I personally prefer using <code>__dirname</code>, because it's somewhat more transparent as to what's happening, and the relative paths consistent with the paths you use in a <code>require</code> statement for the same file.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload