Module loader with aliases
This module tries to solve and improve the module loading. It's very common to have
require()'s with relative paths like
This is a well-known problem known by the Node.js community: Better local require() paths for Node.js. There are some solutions that seem to work but I personally dislike most of them, especially the one which uses the
node_modules directory to store the modules of your app. My advice is to only use
node_modules for external modules, never for storing you own modules.
I'm not going to use this module. This is just a proof of concept. The cleanest way to solve the relative paths is by prefixing them with the
__root variable. In fact, in my opinion Node.js should consider adding it, as it's perfectly aligned with the
The way this module avoids the relative paths is by using more relative paths that shorten the paths and make them relative from anywhere. Think about them as marks, aliases, checkpoints, etc.
Marking a directory
Suppose you have this tree structure:
. ├ a | └ b | └ c | └ file1.js ├ d | └ e | └ f | └ file2.js └ app.js
file1.js if you need to load
file2.js, the path is quite long:
require('../../d/e/f/file2'). What about making the relative paths relative from
.? Put a mark named
my-mark pointing to the directory
// app.jsglobalmod = ;mod;// file1.jsvar f = ;
my-mark, name the mark as
d and you'll have a very descriptive path:
Marking a file
Marks can also point to a file. For example, you have a module you'd like to load with a very short path because it's pretty important, like a module that tells the status of your server:
mod;// Anywhere in your appvar status = ;
You can also use this library to load:
- Any file without marks.
- External and core modules.
Therefore, you can completely replace the
require() function like so:
//The very first line in the entry point of your appglobalmod = ;
Now you can use
mod() from anywhere instead of
require(). In fact, this is the way to use this library.
- module() : Function
- mod() : Any
- mod.mark(marks) : undefined
- mod.resolve(name) : String
- mod.resolveLoose(name) : String
Creates a new namespace for the marks.
var mod = ;
Loads the module. It returns whatever is exported, just like the built-in
require() function. Paths can be any of these:
- System path:
- External module:
- Core module:
If the module name has a mark and is the same as any core module, ie:
util, the core module will be loaded, just the same behaviour as with external modules with the same name as a core module.
Puts marks that point to relative paths. It takes the directory of the current file the reference from which the paths are relative. It can be called multiple times.
//cwd = /foo/bar// ./file1.jsmod;// ./random/path/file2.jsmod;//Anywhere in your appmod; // /foo/bar/a/b/cmod; // /foo/bar/random/path/d/e/f
Returns the absolute path of the module, just like the built-in
require() function, but it also resolves marks.
//cwd = /foo/barmod;mod; // /foo/bar/a/b/c/file.js
resolve() but without any restrictions. The
name can be anything including marks, even if the path doesn't exist it is still resolved.
//cwd = /foo/barmod;mod; // /foo/bar/a/b/c/file.jsmod; // /foo/bar/a/b/c/filemod; // /foo/bar/a/b/c/filemod; // /foo/bar/random/path