Package Exports
- app-module-path
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (app-module-path) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
app-module-path
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 node_modules
directory.
Installation
npm install app-module-path --save
Usage
// ***IMPORTANT**: The following line should be added to the very
// beginning of your main script!
require('app-module-path').addPath(baseDir);
IMPORTANT: The search path should be modified before any modules are loaded!
Example:
In your my-app/index.js
(or my-app/server.js
) file:
// Add the root project directory to the app module search path:
require('app-module-path').addPath(__dirname);
Given the following example directory structure:
- my-app/
- src/ - Source code and application modules directory
- foo/ - A module directory
- index.js
- bar/ - Another module directory
- index.js
- foo/ - A module directory
- node_modules/ - Installed modules
- installed-baz/ - An installed module
- index.js
- installed-baz/ - An installed module
- index.js - Main script
- src/ - Source code and application modules directory
The following will work for any modules under the src
directory:
// All of the following lines will work in "src/foo/index.js" and "src/bar/index.js":
var foo = require('src/foo'); // Works
var bar = require('src/bar'); // Works
var 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'); // Fails
var bar = require('src/bar'); // Fails
Additional Notes
- Search path order:
- App module paths will be added to the beginning of the default module search path. That is, if a module with the same name exists in both a
node_modules
directory and an application module directory then the module in the appliation module directory will be loaded since it is found first.
- App module paths will be added to the beginning of the default module search path. That is, if a module with the same name exists in both a
- Node.js compatibility:
- This module depends on overriding/wrapping a built-in Node.js method, and it is possible (but unlikely) that this behavior could be broken in a future release of Node.js (at which point a workaround would need to be used)
- This module will not change or break modules installed into the
node_modules
directory.
- Recommendations:
- Since this module changes the Node.js convention of how non-relative modules are resolved, it is recommended (but not required) to put all app modules in a common directory below the application root (such as
my-app/src
ormy-app/app_modules
) and then to add the application root to the search path. The require calls would then be something likerequire('src/foo')
orrequire('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 thenode_modules
directory.
- Since this module changes the Node.js convention of how non-relative modules are resolved, it is recommended (but not required) to put all app modules in a common directory below the application root (such as
Contribute
Pull requests, bug reports and feature requests welcome.
License
BSD-2-Clause