reqab
Version:
A simple alias-based module resolver using custom pathconfig.
88 lines (65 loc) โข 1.94 kB
Markdown
custom-require
A utility function to support @alias-based module resolution in Node.js using a custom pathconfig.js.
This module allows you to use custom path aliases (e.g. @utils/logger) instead of relative paths in your Node.js project.
It resolves the alias based on a user-defined pathconfig.js file located in the root directory.
In contrast to the require() function, autocomplete is not supported at the editor level.
## ๐ File Structure Example
```
project-root/
โโโ pathconfig.js
โโโ src/
โ โโโ utils/
โ โโโ logger.js
โโโ index.js
โโโ ...
```
pathconfig.js
```
module.exports = {
aliases: {
utils: "src/utils",
},
};
```
```js
// src/utils/logger.js
module.exports = (msg) => console.log("[LOG]", msg);
```
```js
// service.js
const logger = reqab("@utils/logger");
module.exports = () => {
logger("This works!");
};
// index.js
global.reqab = require("reqab");
const main = require("./service.js"); // reqab doesn't support relative import
main();
```
or
```js
// index.js
const reqab = require("./customRequire");
const logger = reqab("@utils/logger");
logger("This works!");
```
```js
// You can import npm module with reqab
const axios = reqab("axios");
axios.get("https://www.npmjs.com/package/reqab").then((res) => {
console.log(res.status);
});
```
โข If the path starts with / or does not include an alias like @, it behaves like a normal require().
โข If the path contains an alias (e.g. @utils/logger), it:
1. Looks up the alias in pathconfig.js.
2. Resolves the full path based on the current working directory.
3. Requires the actual file at the resolved path.
## โ Error Handling
โข Throws if pathconfig.js is not found.
โข Throws if the provided path is not a string.
โข Throws if an alias is used but not found in pathconfig.js.