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