{"name":"module-alias","version":"2.3.1","keywords":["extend","modules","node","path","resolve"],"author":{"name":"Nick Gavrilov","email":"artnikpro@gmail.com"},"license":"MIT","_id":"module-alias@2.3.1","maintainers":[{"name":"ilearnio","email":"artnikpro@gmail.com"}],"homepage":"https://github.com/ilearnio/module-alias","bugs":{"url":"https://github.com/ilearnio/module-alias/issues"},"dist":{"shasum":"c3216c7f1380bd7d8707c7ff8146efb558c02fe5","tarball":"http://registry-npm.ywwl.com/module-alias/download/module-alias-2.3.1.tgz","fileCount":8,"integrity":"sha512-vqVdHOjoBS8gSpwp24jNpShBPFGYyQI+ANm7g140JEc5muPpSWpOXXhcw13419tLEyM7fl3jZYRORUphKMt+PA==","signatures":[{"sig":"MEUCIDQJWLBOaxxx5MwwHP7EDAuOCPjHA7ESpt+EZdkWBWJvAiEAxuqsb8vbZgvdegdqKBVPPNv6N1wXJ6BqF5FVKGySVpM=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":22662,"size":7872,"noattachment":false,"key":"/module-alias/-/module-alias-2.3.1.tgz"},"main":"index.js","husky":{"hooks":{"pre-push":"npm run test"}},"exports":{".":{"import":"./index.mjs","require":"./index.js"},"./register":{"import":"./register.mjs","require":"./register.js"}},"gitHead":"b226d69b0a3a72ef7343c9829ab59940cc3e5048","scripts":{"lint":"standard","test":"npm run lint && npm run testonly","test:all":"npm run testonly && npm run test:esm","test:esm":"NODE_ENV=test mocha test/esm/unit.mjs test/esm/integration.js","testonly":"NODE_ENV=test mocha test/specs.js","testonly-watch":"NODE_ENV=test mocha -w test/specs.js"},"_npmUser":{"name":"ilearnio","email":"artnikpro@gmail.com"},"repository":{"url":"git+https://github.com/ilearnio/module-alias.git","type":"git"},"_npmVersion":"11.5.1","description":"Create aliases of directories and register custom module paths","directories":{},"_nodeVersion":"24.5.0","_hasShrinkwrap":false,"devDependencies":{"chai":"^3.5.0","husky":"^3.0.2","mocha":"^2.4.5","semver":"^6.1.1","standard":"^12.0.1","hello-world-classic":"github:ilearnio/hello-world-classic"},"_npmOperationalInternal":{"tmp":"tmp/module-alias_2.3.1_1770055842154_0.6426691227221699","host":"s3://npm-registry-packages-npm-production"},"readme":"# module-alias\n\n[![NPM Version][npm-image]][npm-url]\n\nDonations are much appreciated and would help me continue working on this package! [Donate here ❤️](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 long paths in Node, like:\n\n```js\nimport something from ('../../../../some/very/deep/module');\n```\n\nEnough of this madness!\n\nJust create an alias and do it the right way:\n\n```js\nimport module from '@deep/module'\n// Or CommonJS\nconst module = require('@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\nimport module from 'my_private_module'\n// Or CommonJS\nconst module = require('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\n### ES Modules (Node 18.19+)\n\nAdd your custom configuration to your `package.json` (in your application's root):\n\n```json\n{\n  \"_moduleAliases\": {\n    \"@root\": \".\",\n    \"@lib\": \"src/lib\",\n    \"@utils\": \"src/utils\"\n  },\n  \"_moduleDirectories\": [\"node_modules_custom\"]\n}\n```\n\nRun your app with the `--import` flag:\n\n```bash\nnode --import module-alias/register ./app.mjs # Or use a custom registerer, see below\n```\n\n**Why the `--import` flag?**\n\nUnlike CommonJS, you cannot import `module-alias/register` at runtime. All `import` statements are hoisted and resolved before any code runs. The `--import` flag loads the loader hooks before your application starts.\n\n### Programmatic ESM Usage (Node 22.15+):\n\nFor programmatic alias registration, create a custom loader file:\n\n```js\n// my-aliases.mjs\nimport { addAlias, addAliases } from 'module-alias'\n\naddAlias('@utils', process.cwd() + '/src/utils')\n// or\naddAliases({\n  '@utils': process.cwd() + '/src/utils',\n  '@lib': process.cwd() + '/src/lib'\n})\n\n// Custom handler function\naddAlias('@src', (fromPath, request, alias) => {\n  // fromPath - Full path of the file from which `import` was called\n  // request - The path that was passed into `import` (e.g. '@src/utils.js')\n  // alias - The alias being matched (`@src` in this case)\n  const subpath = request.slice(alias.length) // e.g. '/utils.js'\n  const base = fromPath.includes('/tests/') ? '/mocks' : '/src'\n  return process.cwd() + base + subpath\n})\n```\n\nThen use it with the `--import` flag:\n\n```bash\nnode --import ./my-aliases.mjs ./app.mjs\n```\n\n### CommonJS (older Node 12+ versions)\n\nAdd your custom configuration to your `package.json`:\n\n```json\n{\n  \"_moduleAliases\": {\n    \"@root\": \".\",\n    \"@deep\": \"src/some/very/deep/directory/or/file\",\n    \"@my_module\": \"lib/some-file.js\"\n  },\n  \"_moduleDirectories\": [\"node_modules_custom\"]\n}\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\nNow you can use aliases:\n\n```js\nrequire('something')\nconst module = require('@root/some-module')\nconst veryDeepModule = require('@deep/my-module')\n```\n\n## Programmatic CommonJS 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) => {\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/(.*)\": \"<rootDir>/$1\",\n    \"@client/(.*)\": \"<rootDir>/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## 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## 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## Special Thanks\n\nSpecial thanks to [Artur Havrylov](https://github.com/artnikbrothers) and [Daniel Garnier-Moiroux](https://www.npmjs.com/~kehrlann) for valuable contributions.\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","publish_time":1770055842632,"_cnpm_publish_time":1770055842632,"dist-tags":{"latest":"2.3.5"}}