Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are several ways to approach this, each with their own pros and cons:</p> <h2>require.main.filename</h2> <p>From <a href="http://nodejs.org/api/modules.html" rel="noreferrer">http://nodejs.org/api/modules.html</a>:</p> <blockquote> <p>When a file is run directly from Node, <code>require.main</code> is set to its <code>module</code>. That means that you can determine whether a file has been run directly by testing <code>require.main === module</code></p> <p>Because <code>module</code> provides a <code>filename</code> property (normally equivalent to <code>__filename</code>), the entry point of the current application can be obtained by checking <code>require.main.filename</code>.</p> </blockquote> <p>So if you want the base directory for your app, you can do:</p> <pre class="lang-js prettyprint-override"><code>var path = require('path'); var appDir = path.dirname(require.main.filename); </code></pre> <h3>Pros &amp; Cons</h3> <p>This will work great most of the time, but if you're running your app with a launcher like <strong>pm2</strong> or running <strong>mocha</strong> tests, this method will fail.</p> <h2>global.X</h2> <p>Node has a a global namespace object called <code>global</code> — anything that you attach to this object will be available everywhere in your app. So, in your <code>index.js</code> (or <code>app.js</code> or whatever your main app file is named), you can just define a global variable:</p> <pre class="lang-js prettyprint-override"><code>// index.js var path = require('path'); global.appRoot = path.resolve(__dirname); // lib/moduleA/component1.js require(appRoot + '/lib/moduleB/component2.js'); </code></pre> <h3>Pros &amp; Cons</h3> <p>Works consistently but you have to rely on a global variable, which means that you can't easily reuse components/etc.</p> <h2>process.cwd()</h2> <p>This returns the current working directory. Not reliable at all, as it's entirely dependent on what directory the process was launched <strong>from</strong>:</p> <pre class="lang-bash prettyprint-override"><code>$ cd /home/demo/ $ mkdir subdir $ echo "console.log(process.cwd());" &gt; subdir/demo.js $ node subdir/demo.js /home/demo $ cd subdir $ node demo.js /home/demo/subdir </code></pre> <h2>app-root-path</h2> <p>To address this issue, I've created a node module called <strong><a href="https://github.com/inxilpro/node-app-root-path" rel="noreferrer">app-root-path</a></strong>. Usage is simple:</p> <pre class="lang-js prettyprint-override"><code>var appRoot = require('app-root-path'); var myModule = require(appRoot + '/lib/my-module.js'); </code></pre> <p>The <strong><a href="https://github.com/inxilpro/node-app-root-path" rel="noreferrer">app-root-path</a></strong> module uses several different techniques to determine the root path of the app, taking into account globally installed modules (for example, if your app is running in <code>/var/www/</code> but the module is installed in <code>~/.nvm/v0.x.x/lib/node/</code>). It won't work 100% of the time, but it's going to work in most common scenarios.</p> <h3>Pros &amp; Cons</h3> <p>Works without configuration in most circumstances. Also provides some nice additional convenience methods (see project page). The biggest con is that it won't work if:</p> <ul> <li>You're using a launcher, like pm2</li> <li><strong>AND</strong>, the module isn't installed inside your app's <code>node_modules</code> directory (for example, if you installed it globally)</li> </ul> <p>You can get around this by either setting a <code>APP_ROOT_PATH</code> environmental variable, or by calling <code>.setPath()</code> on the module, but in that case, you're probably better off using the <code>global</code> method.</p> <h2>NODE_PATH environmental variable</h2> <p>If you're looking for a way to <em>determine</em> the root path of the current app, one of the above solutions is likely to work best for you. If, on the other hand, you're trying to solve the problem of loading app modules reliably, I highly recommend looking into the <code>NODE_PATH</code> environmental variable.</p> <p>Node's <a href="https://nodejs.org/api/modules.html" rel="noreferrer">Modules system</a> looks for modules in a variety of locations. <a href="https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders" rel="noreferrer">One of these locations is wherever <code>process.env.NODE_PATH</code> points</a>. If you set this environmental variable, then you can <code>require</code> modules with the standard module loader without any other changes.</p> <p>For example, if you set <code>NODE_PATH</code> to <code>/var/www/lib</code>, the the following would work just fine:</p> <pre class="lang-js prettyprint-override"><code>require('module2/component.js'); // ^ looks for /var/www/lib/module2/component.js </code></pre> <p>A great way to do this is using <code>npm</code>:</p> <pre class="lang-js prettyprint-override"><code>"scripts": { "start": "NODE_PATH=. node app.js" } </code></pre> <p>Now you can start your app with <code>npm start</code> and you're golden. I combine this with my <a href="https://github.com/inxilpro/enforce-node-path" rel="noreferrer">enforce-node-path</a> module, which prevents accidentally loading the app without <code>NODE_PATH</code> set. For even more control over enforcing environmental variables, see <a href="https://github.com/inxilpro/node-checkenv" rel="noreferrer">checkenv</a>.</p> <p><strong>One gotcha:</strong> <code>NODE_PATH</code> <strong>must</strong> be set <strong>outside</strong> of the node app. You cannot do something like <code>process.env.NODE_PATH = path.resolve(__dirname)</code> because the module loader caches the list of directories it will search before your app runs.</p> <p><strong>[added 4/6/16]</strong> Another really promising module that attempts to solve this problem is <a href="https://github.com/kolodny/wavy" rel="noreferrer">wavy</a>.</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