Node.js: Where to place internal modules in the folder structure?

Situation

I often see Node.js applications with the following structure:

General pattern:

  • lib/ or src/ - self-written code
    • index.js - main code
    • internal modules ... (for example, written independently for this project)
  • node_modules
    • external modules ... (for example, taken from another project)
  • package.json

My problem

What I don't like about this template:

  • I don’t feel comfortable , because you need to explicitly specify the path to the directory of internal modules when require() ing:
     // /lib/index.js var internalMod = require('./internal'); // `require('internal')` (without path) wouldn't work internalMod.doSomething(); 

My idea

Therefore, I think it would be nice to also place the internal modules in the node_modules folder (somewhere in the project). This way, node can find them even if you do not explicitly specify the path.

For instance:

  • src/ - self-written code
    • index.js - main code
    • node-modules - for internal
      • internal modules ...
  • node_modules - for external
    • external modules ... (for example, taken from another project)
  • package.json

My question

  • Are there any flaws in my plan?
  • Is there any other idea where to place the internal modules in the folder structure?

Thanks for your reply (or comment). - If something is unclear, comment.

+4
source share
1 answer

Perhaps you could use npm link to pull your modules into node_modules? See: https://docs.npmjs.com/cli/link

+2
source

All Articles