Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # module-alias
  2. [![NPM Version][npm-image]][npm-url]
  3. [![Build Status][travis-image]][travis-url]
  4. Create aliases of directories and register custom module paths in NodeJS like a boss!
  5. No more shit-coding paths in Node like so:
  6. ```js
  7. require('../../../../some/very/deep/module')
  8. ```
  9. Enough of this madness!
  10. Just create an alias and do it the right way:
  11. ```js
  12. var module = require('@deep/module')
  13. // Or ES6
  14. import module from '@deep/module'
  15. ```
  16. It 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:
  17. ```js
  18. require('my_private_module');
  19. // Or ES6
  20. import module from 'my_private_module'
  21. ```
  22. **WARNING:** This module should not be used in other npm modules since it modifies the default `require` behavior! It is designed to be used for development of final projects i.e. web-sites, applications etc.
  23. ## Install
  24. ```
  25. npm i --save module-alias
  26. ```
  27. ## Usage
  28. Add your custom configuration to your `package.json` (in your application's root)
  29. ```js
  30. // Aliases
  31. "_moduleAliases": {
  32. "@root" : ".", // Application's root
  33. "@deep" : "src/some/very/deep/directory/or/file",
  34. "@my_module" : "lib/some-file.js",
  35. "something" : "src/foo", // Or without @. Actually, it could be any string
  36. }
  37. // Custom module directories, just like `node_modules` but with your private modules (optional)
  38. "_moduleDirectories": ["node_modules_custom"],
  39. ```
  40. Then add this line at the very main file of your app, before any code
  41. ```js
  42. require('module-alias/register')
  43. ```
  44. **And you're all set!** Now you can do stuff like:
  45. ```js
  46. require('something')
  47. const module = require('@root/some-module')
  48. const veryDeepModule = require('@deep/my-module')
  49. const customModule = require('my_private_module') // module from `node_modules_custom` directory
  50. // Or ES6
  51. import 'something'
  52. import module from '@root/some-module'
  53. import veryDeepModule from '@deep/my-module'
  54. import customModule from 'my_private_module' // module from `node_modules_custom` directory
  55. ```
  56. ## Advanced usage
  57. If 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:
  58. * `addAlias('alias', 'target_path')` - register a single alias
  59. * `addAliases({ 'alias': 'target_path', ... }) ` - register multiple aliases
  60. * `addPath(path)` - Register custom modules directory (like node_modules, but with your own modules)
  61. _Examples:_
  62. ```js
  63. const moduleAlias = require('module-alias')
  64. //
  65. // Register alias
  66. //
  67. moduleAlias.addAlias('@client', __dirname + '/src/client')
  68. // Or multiple aliases
  69. moduleAlias.addAliases({
  70. '@root' : __dirname,
  71. '@client': __dirname + '/src/client',
  72. ...
  73. })
  74. // Custom handler function (starting from v2.1)
  75. moduleAlias.addAlias('@src', (fromPath, request, alias) => {
  76. // fromPath - Full path of the file from which `require` was called
  77. // request - The path (first argument) that was passed into `require`
  78. // alias - The same alias that was passed as first argument to `addAlias` (`@src` in this case)
  79. // Return any custom target path for the `@src` alias depending on arguments
  80. if (fromPath.startsWith(__dirname + '/others')) return __dirname + '/others'
  81. return __dirname + '/src'
  82. })
  83. //
  84. // Register custom modules directory
  85. //
  86. moduleAlias.addPath(__dirname + '/node_modules_custom')
  87. moduleAlias.addPath(__dirname + '/src')
  88. //
  89. // Import settings from a specific package.json
  90. //
  91. moduleAlias(__dirname + '/package.json')
  92. // Or let module-alias to figure where your package.json is
  93. // located. By default it will look in the same directory
  94. // where you have your node_modules (application's root)
  95. moduleAlias()
  96. ```
  97. ## Usage with WebPack
  98. Luckily, 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!
  99. ```js
  100. // webpack.config.js
  101. const npm_package = require('./package.json')
  102. module.exports = {
  103. entry: { ... },
  104. resolve: {
  105. root: __dirname,
  106. alias: npm_package._moduleAliases || {},
  107. modules: npm_package._moduleDirectories || [] // eg: ["node_modules", "node_modules_custom", "src"]
  108. }
  109. }
  110. ```
  111. ## How it works?
  112. In 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.
  113. In 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.
  114. [npm-image]: https://img.shields.io/npm/v/module-alias.svg
  115. [npm-url]: https://npmjs.org/package/module-alias
  116. [travis-image]: https://img.shields.io/travis/ilearnio/module-alias/master.svg
  117. [travis-url]: https://travis-ci.org/ilearnio/module-alias
  118. ## Refactor your code (for already existing projects)
  119. If 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.