d3692519723969eb4c6959ca2845cd3f5fee9e887a862a8e1521d4acf976769da81499f8d6b0657eda09213ed8c88dd928d319e20055d53f87dc87a959eefa 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # which-module
  2. > Find the module object for something that was require()d
  3. [![Build Status](https://travis-ci.org/nexdrew/which-module.svg?branch=master)](https://travis-ci.org/nexdrew/which-module)
  4. [![Coverage Status](https://coveralls.io/repos/github/nexdrew/which-module/badge.svg?branch=master)](https://coveralls.io/github/nexdrew/which-module?branch=master)
  5. [![Standard Version](https://img.shields.io/badge/release-standard%20version-brightgreen.svg)](https://github.com/conventional-changelog/standard-version)
  6. [![Greenkeeper badge](https://badges.greenkeeper.io/nexdrew/which-module.svg)](https://greenkeeper.io/)
  7. Find the `module` object in `require.cache` for something that was `require()`d
  8. or `import`ed - essentially a reverse `require()` lookup.
  9. Useful for libs that want to e.g. lookup a filename for a module or submodule
  10. that it did not `require()` itself.
  11. ## Install and Usage
  12. ```
  13. npm install --save which-module
  14. ```
  15. ```js
  16. const whichModule = require('which-module')
  17. console.log(whichModule(require('something')))
  18. // Module {
  19. // id: '/path/to/project/node_modules/something/index.js',
  20. // exports: [Function],
  21. // parent: ...,
  22. // filename: '/path/to/project/node_modules/something/index.js',
  23. // loaded: true,
  24. // children: [],
  25. // paths: [ '/path/to/project/node_modules/something/node_modules',
  26. // '/path/to/project/node_modules',
  27. // '/path/to/node_modules',
  28. // '/path/node_modules',
  29. // '/node_modules' ] }
  30. ```
  31. ## API
  32. ### `whichModule(exported)`
  33. Return the [`module` object](https://nodejs.org/api/modules.html#modules_the_module_object),
  34. if any, that represents the given argument in the `require.cache`.
  35. `exported` can be anything that was previously `require()`d or `import`ed as a
  36. module, submodule, or dependency - which means `exported` is identical to the
  37. `module.exports` returned by this method.
  38. If `exported` did not come from the `exports` of a `module` in `require.cache`,
  39. then this method returns `null`.
  40. ## License
  41. ISC © Contributors
  42. [opensourceregistry_package_id]: # (458260416784685e5ef3091fee54001785dd4360406aa3000315ff256eef6878)