Note that there are some explanatory texts on larger screens.

plurals
  1. PONode.js: Where To Place Internal Modules In Folder Structure?
    primarykey
    data
    text
    <h2>The Situation</h2> <p>I often see Node.js applications with the following structure:</p> <p><em>Common pattern:</em></p> <ul> <li><code>lib/</code> or <code>src/</code> - the self-written code <ul> <li><code>index.js</code> - main code</li> <li><em>internal modules</em>... (e.g. self-written for this project)</li> </ul></li> <li><code>node_modules</code> <ul> <li><em>external modules</em>... (e.g. taken from another project)</li> </ul></li> <li><code>package.json</code></li> </ul> <h2>My Problem</h2> <p>What I don't like about this pattern:</p> <ul> <li><p>I <strong>don't feel comfortable</strong> about it because you have to <strong>explicitly specify the directory path</strong> of the internal modules when <code>require()</code>ing:</p> <pre><code>// /lib/index.js var internalMod = require('./internal'); // `require('internal')` (without path) wouldn't work internalMod.doSomething(); </code></pre></li> </ul> <h2>My Idea</h2> <p>So I think it would be a good idea also to <strong>place internal modules in an <code>node_modules</code> folder (somewhere</strong> in the project). So <code>node</code> would be able to find them, even if you don't explicitly specify the path.</p> <p><em>For example:</em></p> <ul> <li><code>src/</code> - the self-written code <ul> <li><code>index.js</code> - main code</li> <li><code>node-modules</code> - for internals <ul> <li><em>internal modules</em>...</li> </ul></li> </ul></li> <li><code>node_modules</code> - for externals <ul> <li><em>external modules</em>... (e.g. taken from another project)</li> </ul></li> <li><code>package.json</code></li> </ul> <h2>My Question</h2> <ol> <li><strong>Are there any cons about my plan?</strong></li> <li><strong>Is there another idea where to place internal modules in folder structure?</strong></li> </ol> <p>Thanks for your answer (or comment). - If anything is unclear, please comment.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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