{"id":15040663,"url":"https://github.com/momothepug/tsmodule-alias","last_synced_at":"2025-04-14T18:34:04.466Z","repository":{"id":57127881,"uuid":"122119948","full_name":"momoThePug/tsmodule-alias","owner":"momoThePug","description":"Nodejs Typescript path register","archived":false,"fork":false,"pushed_at":"2018-09-18T20:06:46.000Z","size":93,"stargazers_count":9,"open_issues_count":3,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-28T07:03:30.657Z","etag":null,"topics":["dynamic-aliases","nodejs","path-resolution","tsmodule-alias","typescript","typescript-library","typescript2"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-2.1","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/momoThePug.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}},"created_at":"2018-02-19T20:59:59.000Z","updated_at":"2023-05-11T18:59:27.000Z","dependencies_parsed_at":"2022-08-31T16:11:29.092Z","dependency_job_id":null,"html_url":"https://github.com/momoThePug/tsmodule-alias","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/momoThePug%2Ftsmodule-alias","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/momoThePug%2Ftsmodule-alias/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/momoThePug%2Ftsmodule-alias/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/momoThePug%2Ftsmodule-alias/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/momoThePug","download_url":"https://codeload.github.com/momoThePug/tsmodule-alias/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248937204,"owners_count":21186184,"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":["dynamic-aliases","nodejs","path-resolution","tsmodule-alias","typescript","typescript-library","typescript2"],"created_at":"2024-09-24T20:44:53.576Z","updated_at":"2025-04-14T18:34:04.444Z","avatar_url":"https://github.com/momoThePug.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tsmodule-alias\n\n[![NPM Version][npm-image]][npm-url]\n\n\u003e **NOTE:** Before using any library for aliasing, try to improve your code structure as described [here](https://github.com/Microsoft/TypeScript/issues/9259#issuecomment-379305730) and [here](https://github.com/Microsoft/TypeScript/issues/9259#issuecomment-379372041).\n\n\u003e **NOTE:** Help me improve this project by reporting any issue.\n\nAdds to Node module loader any alias defined in a Typescript configuration file. An example is available at https://github.com/momoThePug/tsmodule-alias-example\n\n## Install\n\n```\nnpm i --save @momothepug/tsmodule-alias\n```\n\n## Usage\n\nAdd these lines to your app's main file, before any code:\n\n**Option A - Alias from Typescript file**\n\n```js\n// www.js, index.js, main.js, etc\nconst TSModuleAlias = require(\"@momothepug/tsmodule-alias\");\n// Path from package.json to your tsconfig.json file\nconst tsconfigToReadFromRoot = \"./\";\n// Makes it work with play method\nconst aliasRegister = TSModuleAlias.play(tsconfigToReadFromRoot);\n// Alias map loaded to nodejs from typescript paths (optional)\nconsole.log(aliasRegister.nodeRegister.aliasMap);\n// Displays root module and typescript project path (optional)\nconsole.log(aliasRegister.currentEnvironmentData);\n```\n\n**Option B - Alias from Typescript file With custom aliases**\n\n```js\n// www.js, index.js, main.js, etc\nconst TSModuleAlias = require(\"@momothepug/tsmodule-alias\");\n// Path from package.json to your tsconfig.json file\nconst tsconfigToReadFromRoot = \"./\";\n// Makes it work with play method, merging custom aliases\nconst aliasRegister = TSModuleAlias.play(tsconfigToReadFromRoot, {\n  \"@crazyAlias\": __dirname + \"/path/to/my/object\"\n});\n// Alias map loaded to nodejs from typescript paths (optional)\nconsole.log(aliasRegister.nodeRegister.aliasMap);\n// Displays root module and typescript project path (optional)\nconsole.log(aliasRegister.currentEnvironmentData);\n```\n\n**Option C - Dynamic aliases on the fly for development purpose**\n\nYou can override an alias value or define a new alias on runtime by invoking `addPathAlias(\"alias\", \"/path/to/your/module\")` just like in our example:\n\n```js\n// www.js, index.js, main.js, etc\nconst TSModuleAlias = require(\"@momothepug/tsmodule-alias\");\n// Path from package.json to your tsconfig.json file\nconst tsconfigToReadFromRoot = \"./\";\n// Makes it work with play method\nconst aliasRegister = TSModuleAlias.play(tsconfigToReadFromRoot);\n// Defining/overriding an alias programatically, path value must be an absolute path\naliasRegister.addPathAlias(\n  \"@my_dynamic_alias\",\n  __dirname + \"/leo/orange/dog/orange\"\n);\n```\n\n**Option D - Dynamic aliases on the fly**\n\nYou can register aliases using an object like the following example:\n\n```js\nconst tSModuleAlias = require(\"@momothepug/tsmodule-alias\");\n\n// make it work using custom alias before execution\nconst aliasRegister = tSModuleAlias.use({\n  \"@leoAlias\": __dirname + \"/leo/orange/dog/orange\",\n  \"@pugpath/pug\": __dirname + \"/myfooobar/func\",\n  \"@bar\": __dirname + \"/bar/bar\"\n});\n\nconsole.log(require(\"@bar\"));\nconsole.log(require(\"@pugpath/pug\")(\"Jhon Doe\"));\nconsole.log(require(\"@leoAlias\"));\n```\n\n**Option E - Alias register resolving tsconfig automatically**\n\nYou can initialize typescript alias, resolving tsconfig file automatically:\n\n```js\n// www.js, index.js, main.js, etc\nconst TSModuleAlias = require(\"@momothepug/tsmodule-alias\");\n// Makes it work with playAuto method\n// this method Will scan backward until tsconfig is found\nconst aliasRegister = TSModuleAlias.playAuto(__dirname);\n```\n\n## Path resolution strategy\n\nOnly index zero will be used for path resolution in any alias definition. Ej:\n\n```js\n//  tsmodule-alias will resolve @root/* as \"./src/*\"\n\"@root/*\": [\"./src/*\", \"./moresrc/*\", \"./momosrc/*\"]\n```\n\n## Nodejs \u0026 Typescript problem background\n\nTo avoid the path hell in typescript like the following example:\n\n```typescript\nimport { MyClass } from \"../../../my/own/module\";\n```\n\nWe can define aliases in tsconfig.json file using \"paths\" \u0026 \"baseUrl\" indexes:\n\n```json\n{\n  \"compilerOptions\": {\n    \"target\": \"es6\",\n    \"module\": \"commonjs\",\n    \"outDir\": \"./\",\n    \"strict\": false,\n    \"baseUrl\": \"./\",\n    \"declaration\": true,\n    \"experimentalDecorators\": true,\n    \"emitDecoratorMetadata\": true,\n    \"paths\": {\n      \"@root/*\": [\"./src/*\"],\n      \"@test/*\": [\"./src/test/*\"],\n      \"@mybeautifulModule\": [\"./src/modules/my/module\"],\n      \"@deepmodule\": [\"./../../../my/own/module\"]\n    }\n  },\n  \"include\": [\"./\", \"src/**/*\"],\n  \"exclude\": [\"node_modules\", \"dist\"]\n}\n```\n\nThat way we can use our aliases in any import:\n\n```typescript\nimport { MyClass } from \"@deepmodule\";\n```\n\n**But...**\n\nThere is a problem when you compile a typescript project to be consumed by a Node interpreter: Node cannot understand what the hell are typescript **Path Aliases**!. To solve that you can use tsmodule-alias to read and load aliases from your tsconfig file.\n\n**Nodejs Error thrown by the interpreter (example)**\n\n```\n   module.js:471\n    throw err;\n    ^\n\nError: Cannot find module '@CrazyAliasDefinedInTypescriptAliasConfig'\n    at Function.Module._resolveFilename (module.js:469:15)\n    at Function.Module._load (module.js:417:25)\n    at Module.require (module.js:497:17)\n    at require (internal/module.js:20:19)\n    at Object.\u003canonymous\u003e (/home/youruser/crazy/path/you-mod/index.js:7:13)\n    at Module._compile (module.js:570:32)\n    at Object.Module._extensions..js (module.js:579:10)\n    at Module.load (module.js:487:32)\n    at tryModuleLoad (module.js:446:12)\n    at Function.Module._load (module.js:438:3)\n\nnpm ERR! CrazyOS 4.14.14-200.fc26.x86_64\nnpm ERR! argv \"/usr/bin/node\" \"/usr/bin/npm\" \"run\" \"start\"\nnpm ERR! node v6.12.0\nnpm ERR! npm  v3.10.10\nnpm ERR! code ELIFECYCLE\nnpm ERR! you-mod@1.0.0 start: ` npm run build \u0026\u0026 node index.js `\nnpm ERR! Exit status 1\nnpm ERR!\nnpm ERR! Failed at the you-mod@1.0.0 start script ' npm run build \u0026\u0026 node index.js '.\nnpm ERR! Make sure you have the latest version of node.js and npm installed.\nnpm ERR! If you do, this is most likely a problem with the you-mod package,\nnpm ERR! not with npm itself.\nnpm ERR! Tell the author that this fails on your system:\nnpm ERR!      npm run build \u0026\u0026 node index.js\nnpm ERR! You can get information on how to open an issue for this project with:\nnpm ERR!     npm bugs you-mod\nnpm ERR! Or if that isn't available, you can get their info via:\nnpm ERR!     npm owner ls you-mod\nnpm ERR! There is likely additional logging output above.\n\nnpm ERR! Please include the following file with any support request:\nnpm ERR!     /home/youruser/crazy/path/you-mod/npm-debug.log\n```\n\n:p Be happy\n\n[npm-image]: https://img.shields.io/npm/v/@momothepug/tsmodule-alias.svg\n[npm-url]: https://npmjs.org/package/@momothepug/tsmodule-alias\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmomothepug%2Ftsmodule-alias","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmomothepug%2Ftsmodule-alias","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmomothepug%2Ftsmodule-alias/lists"}