[Node.js] Alias
Library that might help you deal with Node paths
npm install require-alias
Table of Contents
Quick example
(Of how one might use it in project)
Simple application with three files and some folders with structure like:
C:\test
- the application folder
C:\test\this\is\an\example.js
- random export of function that returns string 'foo'
module { return 'foo';};
C:\test\app.js
- the main runnable where to configure alias
var Alias = ; globalalias = aliases: '@root': './' // will point to C:\test\ '@folder': 'this/is/an/' // will point to C:\test\this\is\an\ ; alias;
C:\test\random.js
- just a random file to test alias
// Can be done in any file as long as aliases are defined first and assigned to globalvar path = alias;console; // Output: C:\test\this\is\an\example var module = alias;console; // Output: foo
Full example
/** * Require and configure Alias with initial options */ var Alias = ; var exampleAlias = root: './app/' aliases: '@models': 'models' '@random': 'random' '@another': 'another/path' '@and': 'another/path' ; /** * Add some more aliases */ exampleAlias; /** * Add single alias */ exampleAlias; /** * Delete single alias */ exampleAlias; /** * Delete multiple aliases */ exampleAlias; /** * Get path to foo using alias + path */ var pathToFoo = exampleAlias;var foo = ;console; // Output: Foo /** * Get path to bar with using only alias */ var pathToBar = exampleAlias;var bar = ;console; // Output: Bar /** * Require module using alias */ var moduleFoo = exampleAlias;console; // Output: Foo /** * This is the basic usage. * * You can also use it in other ways to make your life easier. Say you don't like * the functionality of default require and do not wish to write most of the time * * require(alias('@foo/bar')) * * You may assign alias to global variable and use it instead of require: */ globalalias = exampleAlias; /** * Just an example */ alias;var baz = alias;console; // Output: Baz var pathToBaz = alias;console; // Output: C:\require-alias\example\app\baz (Or wherever // your project is located /** * Note * * Whether you assign alias to global variable or not is up to you. Even though it is * highly recommended (and also a good practice) to not assign anything to global * variables, this might be a good candidate for global scope. The intent of this * "helper" was to make requiring modules and paths easier, maybe even replace the * majority of cases where "require" is used. * */
API
var alias = ;
alias.root.set(String)
String
- New root path
alias.root.get()
- Returns current root
alias.storage(String)
Returns path of given alias or the whole storage
String
(optional) - Alias (eg'@foo'
)
alias.add(String, String)
Add single alias
-
String
- Alias (eg'@foo'
) -
String
- Path reference of alias (eg'../folder/another/folder'
)
alias.add(Object)
Add single / multiple alias(es)
Object
- Key-value object where key is alias and value is path reference alias (eg{@foo: 'foo/bar'}
)
alias.delete(String)
Deletes single alias
String
- Alias (eg'@foo'
)
alias.delete(Array)
Deletes multiple aliases
Array
- Array of strings, each being alias (eg['@foo', '@bar']
)
alias.path(String)
Returns path reference of alias
String
- Alias (eg'@foo'
)
alias.require(String)
Returns module export of alias
String
- Alias (eg'@foo'
)