Simple module to add additional directories to the Node module search for top-level app modules
This simple module enables you to add additional directories to the Node.js module search path (for top-level app modules only). This allows application-level modules to be required as if they were installed into the
npm install app-module-path --save
// ***IMPORTANT**: The following line should be added to the very// beginning of your main script!require'app-module-path'addPathbaseDir;
IMPORTANT: The search path should be modified before any modules are loaded!
// Add the root project directory to the app module search path:require'app-module-path'addPath__dirname;
Given the following example directory structure:
The following will work for any modules under the
// All of the following lines will work in "src/foo/index.js" and "src/bar/index.js":var foo = require'src/foo'; // Worksvar bar = require'src/bar'; // Worksvar baz = require'installed-baz'; // Works
Lastly, by design, installed modules (i.e. modules under the
node_modules directory) will not be able to require application-level modules so the following will not work:
// All of the following lines will work *not* work in "node_modules/installed-baz/index.js"!var foo = require'src/foo'; // Failsvar bar = require'src/bar'; // Fails
node_modulesdirectory and an application module directory then the module in the appliation module directory will be loaded since it is found first.
my-app/app_modules) and then to add the application root to the search path. The require calls would then be something like
require('app_modules/foo'). The common prefix makes it more clear that the module can be found in the application's modules directory and not in the
Pull requests, bug reports and feature requests welcome.