app_modules
Get rid of relative import syntax by making app subdirectories appear as modules.
E.g.,
;// instead of;
Works seemlessly with transpiled code (see below).
Install
npm install app_modules
Configure
Run code like the following as early as possible in your app:
// before requiring other files...var app_modules = ; // IRL, compute the following paths dynamically: // initialize the library (this creates an app_modules dir in your project root):app_modules; // make src/ available as a module named app:app_modules; // app_modules/app now points to src/
Example
Given a Node.js project as follows:
- myproject - package.json - src/ - main.js # start script - foo/ - bar.js - lib/ # transpiled code
Initialize app_modules with dynamically computed paths:
// src/main.jsvar app_modules = ;var path = ; app_modules; // path of project rootapp_modules; // dynamically link app to either src/ or lib/ // ... continue loading
Now you can write:
var bar = ;
The app
module links correctly to src/
or lib/
depending on whether you're running the untranspiled src/main.js
or transpiled lib/main.js
code.
Warning
This library uses an old but unofficial method _initPaths
defined on the Modules
class.
API
init
Ensures a directory exists in the project root and adds it to the NODE_PATH. Directories in this folder will be accessible as modules.
// where// projectRoot - path to the root dir of your node project// app_modules_dirname - defaults to 'app_modules'
addModule
Symlinks a directory into your app_modules directory.
// where// moduleName - name you'd like the sourcePath to be accessible as// sourcePath - absolute path to a directory