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.

55 lines
1.9 KiB

4 years ago
  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. Find the `module` object in `require.cache` for something that was `require()`d
  7. or `import`ed - essentially a reverse `require()` lookup.
  8. Useful for libs that want to e.g. lookup a filename for a module or submodule
  9. that it did not `require()` itself.
  10. ## Install and Usage
  11. ```
  12. npm install --save which-module
  13. ```
  14. ```js
  15. const whichModule = require('which-module')
  16. console.log(whichModule(require('something')))
  17. // Module {
  18. // id: '/path/to/project/node_modules/something/index.js',
  19. // exports: [Function],
  20. // parent: ...,
  21. // filename: '/path/to/project/node_modules/something/index.js',
  22. // loaded: true,
  23. // children: [],
  24. // paths: [ '/path/to/project/node_modules/something/node_modules',
  25. // '/path/to/project/node_modules',
  26. // '/path/to/node_modules',
  27. // '/path/node_modules',
  28. // '/node_modules' ] }
  29. ```
  30. ## API
  31. ### `whichModule(exported)`
  32. Return the [`module` object](https://nodejs.org/api/modules.html#modules_the_module_object),
  33. if any, that represents the given argument in the `require.cache`.
  34. `exported` can be anything that was previously `require()`d or `import`ed as a
  35. module, submodule, or dependency - which means `exported` is identical to the
  36. `module.exports` returned by this method.
  37. If `exported` did not come from the `exports` of a `module` in `require.cache`,
  38. then this method returns `null`.
  39. ## License
  40. ISC © Contributors