{"id":15612480,"url":"https://github.com/euberdeveloper/esm-module-alias","last_synced_at":"2025-02-27T20:15:34.617Z","repository":{"id":48137528,"uuid":"515361525","full_name":"euberdeveloper/esm-module-alias","owner":"euberdeveloper","description":"An alternative to module-alias, but for esm","archived":false,"fork":false,"pushed_at":"2024-08-27T21:53:39.000Z","size":331,"stargazers_count":34,"open_issues_count":0,"forks_count":5,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-02-20T19:12:31.826Z","etag":null,"topics":["esm","nodejs","npm"],"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/euberdeveloper.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","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":"2022-07-18T22:36:32.000Z","updated_at":"2024-11-24T21:07:18.000Z","dependencies_parsed_at":"2023-02-16T20:16:07.546Z","dependency_job_id":"578d4c10-06f3-4e04-8482-103d2af72a88","html_url":"https://github.com/euberdeveloper/esm-module-alias","commit_stats":{"total_commits":26,"total_committers":1,"mean_commits":26.0,"dds":0.0,"last_synced_commit":"1bd778846a81d7be3396d7233d3ce117e5a878a7"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/euberdeveloper%2Fesm-module-alias","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/euberdeveloper%2Fesm-module-alias/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/euberdeveloper%2Fesm-module-alias/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/euberdeveloper%2Fesm-module-alias/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/euberdeveloper","download_url":"https://codeload.github.com/euberdeveloper/esm-module-alias/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240980071,"owners_count":19888188,"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":["esm","nodejs","npm"],"created_at":"2024-10-03T06:43:12.221Z","updated_at":"2025-02-27T20:15:34.597Z","avatar_url":"https://github.com/euberdeveloper.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)\n[![License](https://img.shields.io/npm/l/esm-module-alias.svg)](https://github.com/euberdeveloper/esm-module-alias/blob/master/LICENSE)\n[![GitHub issues](https://img.shields.io/github/issues/euberdeveloper/esm-module-alias.svg)](https://github.com/euberdeveloper/esm-module-alias/issues)\n[![GitHub stars](https://img.shields.io/github/stars/euberdeveloper/esm-module-alias.svg)](https://github.com/euberdeveloper/esm-module-alias/stargazers)\n![npm](https://img.shields.io/npm/v/esm-module-alias.svg)\n\n\n# esm-module-alias\nAn alternative to module-alias, but for esm\n\n## Project purpose\n\nThe purpose of this project is allowing developers that use **esm** modules to have a feature similar to the one provided by **[module alias](https://www.npmjs.com/package/module-alias)**.\n\n*module-alias* provides the possibility to alias modules to a different path, taking the same example that is used in its documentation:\n\n```js\nrequire('../../../../some/very/deep/module');\n```\n\ncan become:\n\n```js\nimport module from '@deep/module';\n```\n\nTo allow this, one should add some paths to the `package.json`, like:\n```json\n{\n    \"aliases\": {\n        \"@deep\": \"src/some/very/deep\"\n    }\n}\n```\n\nThe module stopped working after the introduction of the [**esm**](https://nodejs.org/api/esm.html) in NodeJS. In addition, at the moment in which this README was written, *module-alias* was last published three years ago.\n\n## How to pass to esm\n\nTaken from this [fantastic guide](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):\n\n- Add `\"type\": \"module\"` to your package.json.\n- Replace `\"main\": \"index.js\"` with `\"exports\": \"./index.js\"` in your package.json.\n- Update the `\"engines\"` field in package.json to Node.js 14: `\"node\": \"\u003e=14.16\"`. (Excluding Node.js 12 as it's no longer supported)\n- Remove `'use strict';` from all JavaScript files.\n- Replace all `require()`/`module.export` with `import`/`export`.\n- Use only full relative file paths for imports: `import x from '.';` → `import x from './index.js';`.\n- If you have a TypeScript type definition (for example, `index.d.ts`), update it to use ESM imports/exports.\n- Optional but recommended, use the [`node:` protocol](https://nodejs.org/api/esm.html#esm_node_imports) for imports.\n\n## How to use this module to continue using the module aliases\n\nTo use this module:\n* Install the module by exeuting \n  ```bash\n  $ npm install esm-module-alias\n  ```\n* Add the property `aliases` to the `package.json`, the same way you would have done with `module-alias`, for example:\n  ```json\n  {\n    \"aliases\": {\n        \"@root\"      : \".\",\n        \"@deep\"      : \"src/some/very/deep/directory/or/file\",\n        \"@my_module\" : \"lib/some-file.js\",\n        \"something\"  : \"src/foo\"\n    }\n  }\n  ```\n* When you execute your script, **add this module as a loader** by adding `--loader esm-module-alias/loader`, for example:\n    ```bash\n    node --loader esm-module-alias/loader --no-warnings myscript.js # Note that --no-warnings is to disable a warning and is optional\n    ```\n\n## An option if you want to create a custom loader\n\nYou can also create a custom loader, because the library exports a function that given an object like the `aliases` one that one would define in the `package.json`, will return a function that will be used as a loader.\n\nTo do so:\n\n* Create a custom file named as you want, for instance `my-loader.mjs`:\n  ```js\n  import generateAliasesResolver from 'esm-module-alias'; \n  const aliases = {\n    \"@root\": \".\",\n    \"@deep\": \"src/some/very/deep/directory/or/file\",\n    \"@my_module\": \"lib/some-file.js\",\n    \"something\": \"src/foo\"\n  };\n  export const resolve = generateAliasesResolver(aliases);\n  ```\n* When you execute your script, **add that script as a loader** by adding `--loader ./my-loader.mjs`, for example:\n    ```bash\n    node --loader=./my-loader.mjs --no-warnings myscript.js # Note that --no-warnings is to disable a warning and is optional\n    ```\n    \n## What if you want to change the matching behaviour?\n\nYou can also have a custom matcher, a function that customize the behaviour of a path matching an alias.\n\nTo do so:\n\n* Create a custom file named as you want, for instance `my-loader.mjs`:\n  ```js\n  import generateAliasesResolver from 'esm-module-alias'; \n  const aliases = {\n    \"@root\": \".\",\n    \"@deep\": \"src/some/very/deep/directory/or/file\",\n    \"@my_module\": \"lib/some-file.js\",\n    \"something\": \"src/foo\"\n  };\n  const matcher = (path, alias) =\u003e {\n    return (path.indexOf(alias) === 0); // Your customized code\n  }; \n  export const resolve = generateAliasesResolver(aliases, { matcher }); // The custom matcher is passed to the options\n  ```\n* When you execute your script, **add that script as a loader** by adding `--loader ./my-loader.mjs`, for example:\n    ```bash\n    node --loader=./my-loader.mjs --no-warnings myscript.js # Note that --no-warnings is to disable a warning and is optional\n    ```\n\n## Tests\n\nTests are run with **[Jest](https://jestjs.io/)** and work by executing `npm test` with **[shelljs](https://https://www.npmjs.com/package/shelljs)** on a bunch of sample projects,\n\n## Note\n\nThis package took inspiration from [a comment of a Github issue](https://github.com/ilearnio/module-alias/issues/59#issuecomment-500480450)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feuberdeveloper%2Fesm-module-alias","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feuberdeveloper%2Fesm-module-alias","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feuberdeveloper%2Fesm-module-alias/lists"}