{"id":13440268,"url":"https://github.com/ilearnio/module-alias","last_synced_at":"2025-05-14T02:00:26.256Z","repository":{"id":41443331,"uuid":"52159299","full_name":"ilearnio/module-alias","owner":"ilearnio","description":"Register aliases of directories and custom module paths in Node","archived":false,"fork":false,"pushed_at":"2024-04-11T04:42:39.000Z","size":85,"stargazers_count":1780,"open_issues_count":55,"forks_count":71,"subscribers_count":9,"default_branch":"dev","last_synced_at":"2025-05-06T02:40:08.110Z","etag":null,"topics":["aliases","module","node","nodejs"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ilearnio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-02-20T15:12:25.000Z","updated_at":"2025-04-25T23:31:31.000Z","dependencies_parsed_at":"2024-06-18T11:14:14.736Z","dependency_job_id":"81d6be3d-8394-4445-8743-b5fbe811216d","html_url":"https://github.com/ilearnio/module-alias","commit_stats":{"total_commits":82,"total_committers":11,"mean_commits":7.454545454545454,"dds":0.4390243902439024,"last_synced_commit":"aa0d1429780a843773682cd60f309ec240c093e9"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ilearnio%2Fmodule-alias","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ilearnio%2Fmodule-alias/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ilearnio%2Fmodule-alias/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ilearnio%2Fmodule-alias/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ilearnio","download_url":"https://codeload.github.com/ilearnio/module-alias/tar.gz/refs/heads/dev","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253068804,"owners_count":21848870,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aliases","module","node","nodejs"],"created_at":"2024-07-31T03:01:21.201Z","updated_at":"2025-05-14T02:00:26.229Z","avatar_url":"https://github.com/ilearnio.png","language":"JavaScript","funding_links":[],"categories":["HarmonyOS","JavaScript","nodejs"],"sub_categories":["Windows Manager"],"readme":"# module-alias\n[![NPM Version][npm-image]][npm-url]\n\nIf everyone who reads this would donate just $1, I would be a millionaire in 1 week! 🙃 Thank you for reaching 1M+ weekly downloads!\n\nMore donations means more motivation for me to make updates. Thank you so much!\n\n[DONATE $1 ❤️](https://tinyurl.com/donate-module-alias)\n\n---\n\nCreate aliases of directories and register custom module paths in NodeJS like a boss!\n\nNo more shit-coding paths in Node like so:\n\n```js\nrequire('../../../../some/very/deep/module')\n```\nEnough of this madness!\n\nJust create an alias and do it the right way:\n\n```js\nvar module = require('@deep/module')\n// Or ES6\nimport module from '@deep/module'\n```\n\nIt also allows you to register directories that will act just like `node_modules` but with your own private modules, so that you can access them directly:\n\n```js\nrequire('my_private_module');\n// Or ES6\nimport module from 'my_private_module'\n```\n\n**WARNING:** If you are going to use this package within another NPM package, please read [Using within another NPM package](#using-within-another-npm-package) first to be aware of potential caveats.\n\n## Install\n\n```\nnpm i --save module-alias\n```\n\n## Usage\n\nAdd your custom configuration to your `package.json` (in your application's root)\n\n```js\n// Aliases\n\"_moduleAliases\": {\n  \"@root\"      : \".\", // Application's root\n  \"@deep\"      : \"src/some/very/deep/directory/or/file\",\n  \"@my_module\" : \"lib/some-file.js\",\n  \"something\"  : \"src/foo\", // Or without @. Actually, it could be any string\n}\n\n// Custom module directories, just like `node_modules` but with your private modules (optional)\n\"_moduleDirectories\": [\"node_modules_custom\"],\n```\n\nThen add this line at the very main file of your app, before any code\n\n```js\nrequire('module-alias/register')\n```\n\n**And you're all set!** Now you can do stuff like:\n\n```js\nrequire('something')\nconst module = require('@root/some-module')\nconst veryDeepModule = require('@deep/my-module')\nconst customModule = require('my_private_module') // module from `node_modules_custom` directory\n\n// Or ES6\nimport 'something'\nimport module from '@root/some-module'\nimport veryDeepModule from '@deep/my-module'\nimport customModule from 'my_private_module' // module from `node_modules_custom` directory\n```\n\n## Advanced usage\n\nIf 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:\n\n* `addAlias('alias', 'target_path')` - register a single alias\n* `addAliases({ 'alias': 'target_path', ... }) ` - register multiple aliases\n* `addPath(path)` - Register custom modules directory (like node_modules, but with your own modules)\n\n_Examples:_\n```js\nconst moduleAlias = require('module-alias')\n\n//\n// Register alias\n//\nmoduleAlias.addAlias('@client', __dirname + '/src/client')\n\n// Or multiple aliases\nmoduleAlias.addAliases({\n  '@root'  : __dirname,\n  '@client': __dirname + '/src/client',\n  ...\n})\n\n// Custom handler function (starting from v2.1)\nmoduleAlias.addAlias('@src', (fromPath, request, alias) =\u003e {\n  // fromPath - Full path of the file from which `require` was called\n  // request - The path (first argument) that was passed into `require`\n  // alias - The same alias that was passed as first argument to `addAlias` (`@src` in this case)\n\n  // Return any custom target path for the `@src` alias depending on arguments\n  if (fromPath.startsWith(__dirname + '/others')) return __dirname + '/others'\n  return __dirname + '/src'\n})\n\n//\n// Register custom modules directory\n//\nmoduleAlias.addPath(__dirname + '/node_modules_custom')\nmoduleAlias.addPath(__dirname + '/src')\n\n//\n// Import settings from a specific package.json\n//\nmoduleAlias(__dirname + '/package.json')\n\n// Or let module-alias to figure where your package.json is\n// located. By default it will look in the same directory\n// where you have your node_modules (application's root)\nmoduleAlias()\n```\n\n## Usage with WebPack\n\nLuckily, WebPack has a built in support for aliases and custom modules directories so it's easy to make it work on the client side as well!\n\n```js\n// webpack.config.js\nconst npm_package = require('./package.json')\n\nmodule.exports = {\n  entry: { ... },\n  resolve: {\n    root: __dirname,\n    alias: npm_package._moduleAliases || {},\n    modules: npm_package._moduleDirectories || [] // eg: [\"node_modules\", \"node_modules_custom\", \"src\"]\n  }\n}\n```\n\nMore details on the [official documentation](https://webpack.js.org/configuration/resolve).\n\n## Usage with Jest\n\nUnfortunately, `module-alias` itself would not work from Jest due to a custom behavior of Jest's `require`. But you can use it's own aliasing mechanism instead. The configuration can be defined either in `package.json` or `jest.config.js`. The example below is for `package.json`:\n\n```js\n\"jest\": {\n  \"moduleNameMapper\": {\n    \"@root/(.*)\": \"\u003crootDir\u003e/$1\",\n    \"@client/(.*)\": \"\u003crootDir\u003e/src/client/$1\"\n  },\n}\n```\n\nMore details on the [official documentation](https://jestjs.io/docs/en/configuration#modulenamemapper-objectstring-string--arraystring).\n\n## Using within another NPM package\n\nYou can use `module-alias` within another NPM package, however there are a few things to take into consideration.\n\n1. As the aliases are global, you should make sure your aliases are unique, to avoid conflicts with end-user code, or with other libraries using module-alias. For example, you could prefix your aliases with '@my-lib/', and then use require('@my-lib/deep').\n2. The internal \"register\" mechanism may not work, you should not rely on `require('module-alias/register')` for automatic detection of `package.json` location (where you defined your aliases), as it tries to find package.json in either the current working directory of your node process, or two levels down from node_modules/module-alias. It is extremely likely that this is end-user code. So, instead, your should either register aliases manually with `moduleAlias.addAlias`, or using something like `require('module-alias')(__dirname)`.\n\nHere is an [example project](https://github.com/Kehrlann/module-alias-library).\n\n\n## Known incompatibilities\n\nThis module does not play well with:\n\n- Front-end JavaScript code. Module-alias is designed for server side so do not expect it to work with front-end frameworks (React, Vue, ...) as they tend to use Webpack. Use Webpack's [resolve.alias](https://webpack.js.org/configuration/resolve/#resolvealias) mechanism instead.\n- [Jest](https://jestjs.io), which discards node's module system entirely to use it's own module system, bypassing module-alias.\n- The [NCC compiler](https://github.com/zeit/ncc), as it uses WebPack under the hood without exposing properties, such as resolve.alias. It is not [something they wish to do](https://github.com/zeit/ncc/pull/460).\n\n## How it works?\n\nIn order to register an alias it modifies the internal `Module._resolveFilename` method so that when you use `require` or `import` it first checks whether the given string starts with one of the registered aliases, if so, it replaces the alias in the string with the target path of the alias.\n\nIn order to register a custom modules path (`addPath`) it modifies the internal `Module._nodeModulePaths` method so that the given directory then acts like it's the `node_modules` directory.\n\n## Refactor your code (for already existing projects)\n\nIf you are using this on an existing project, you can use [relative-to-alias](https://github.com/s-yadav/relative-to-alias) to refactor your code to start using aliases.\n\n## Donate\n\nIf everyone who downloads module-alias would donate just $1, I would be a millionaire in 1 week!\n\nI love contributing to open source, for free, but you know, sometimes, in the middle of the night, I may wan to eat.\n\nThere are some improvements planned for module-alias and your donations will help a lot to make it happen faster.\n\n[DONATE $1 ❤️](https://tinyurl.com/donate-module-alias) and thank you so much!\n\n\n[npm-image]: https://img.shields.io/npm/v/module-alias.svg\n[npm-url]: https://npmjs.org/package/module-alias\n[travis-image]: https://img.shields.io/travis/ilearnio/module-alias/master.svg\n[travis-url]: https://travis-ci.org/ilearnio/module-alias\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Filearnio%2Fmodule-alias","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Filearnio%2Fmodule-alias","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Filearnio%2Fmodule-alias/lists"}