npm install load-alias
This module requires files via aliases
const LoadAlias = require('load-alias');
const loadConfig = {
public: 'public/',
css: '@public/css/',
img: '@public/img/',
favicon: '@img/favicon.ico',
db: 'db/',
dbModels: '@db/models/',
upload: 'upload/',
test: function (alias, replacement) {
return 'some/thing';
}
}
const alias = new LoadAlias(loadConfig); // or new loadAlias(rootPath, loadConfig)
let modelUser = alias.load('@dbModels/user.js');
let imgAvatarPath = alias.getPath('@img/avatar.png'); // => 'public/img/avatar.png'
let imgAvatarFullPath = alias.getFullPath('@img/avatar.png'); // => alias.rootPath + '/public/img/avatar.png'
let path = alias.require('path'); // gets nodejs path library
let something = alias.require('./relative/path/'); // doesn't merge with .rootPath
The default alias.rootPath
= process.cwd()
But you can change it: alias.rootPath = something
Loading file using a full path
Loading file using a relative path.
Getting the config
Config will be merged with the current
Getting the alias
Adding or updating the alias
Deleting the alias
Getting transformed path without .rootPath
Getting the full transformed path including .rootPath