@esmkit/alias
v0.1.1
Published
Create aliases of directories and register custom module paths in NodeJS like a boss!
Readme
@esmkit/alias
Create aliases of directories and register custom module paths in NodeJS like a boss!
Installation
bun add @esmkit/aliasAdvanced usage
If you don't want to modify your package.json or you just prefer to set it all up programmatically, then the following methods are available for you:
addAlias('alias', 'target_path')- register a single aliasaddAliases({ 'alias': 'target_path', ... })- register multiple aliasesaddPath(path)- Register custom modules directory (like node_modules, but with your own modules)
Examples:
import alias from '@esmkit/alias'
alias.init()
//
// Register alias
//
alias.addAlias('@client', __dirname + '/src/client')
// Or multiple aliases
alias.addAliases({
'@root' : __dirname,
'@client': __dirname + '/src/client',
})
alias.addAlias('@src', (fromPath, request, alias) => {
// fromPath - Full path of the file from which `require` was called
// request - The path (first argument) that was passed into `require`
// alias - The same alias that was passed as first argument to `addAlias` (`@src` in this case)
// Return any custom target path for the `@src` alias depending on arguments
if (fromPath.startsWith(__dirname + '/others')) return __dirname + '/others'
return __dirname + '/src'
})
//
// Register custom modules directory
//
alias.addPath(__dirname + '/node_modules_custom')
alias.addPath(__dirname + '/src')
//
// Import settings from a specific package.json
//
alias(__dirname + '/package.json')
// Or let module-alias to figure where your package.json is
// located. By default it will look in the same directory
// where you have your node_modules (application's root)
alias()License
MIT © BILLGO. See LICENSE for details.
