https://github.com/nexdrew/which-module
:m: Find the module object for something that was require()d
https://github.com/nexdrew/which-module
import lookup module nodejs require reverse
Last synced: 6 months ago
JSON representation
:m: Find the module object for something that was require()d
- Host: GitHub
- URL: https://github.com/nexdrew/which-module
- Owner: nexdrew
- License: isc
- Created: 2016-06-06T05:39:02.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-04-21T14:38:21.000Z (over 2 years ago)
- Last Synced: 2025-04-06T06:17:00.454Z (6 months ago)
- Topics: import, lookup, module, nodejs, require, reverse
- Language: JavaScript
- Size: 38.1 KB
- Stars: 27
- Watchers: 1
- Forks: 4
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# which-module
> Find the module object for something that was require()d
[](https://travis-ci.org/nexdrew/which-module)
[](https://coveralls.io/github/nexdrew/which-module?branch=master)
[](https://github.com/conventional-changelog/standard-version)
[](https://greenkeeper.io/)Find the `module` object in `require.cache` for something that was `require()`d
or `import`ed - essentially a reverse `require()` lookup.Useful for libs that want to e.g. lookup a filename for a module or submodule
that it did not `require()` itself.## Install and Usage
```
npm install --save which-module
``````js
const whichModule = require('which-module')console.log(whichModule(require('something')))
// Module {
// id: '/path/to/project/node_modules/something/index.js',
// exports: [Function],
// parent: ...,
// filename: '/path/to/project/node_modules/something/index.js',
// loaded: true,
// children: [],
// paths: [ '/path/to/project/node_modules/something/node_modules',
// '/path/to/project/node_modules',
// '/path/to/node_modules',
// '/path/node_modules',
// '/node_modules' ] }
```## API
### `whichModule(exported)`
Return the [`module` object](https://nodejs.org/api/modules.html#modules_the_module_object),
if any, that represents the given argument in the `require.cache`.`exported` can be anything that was previously `require()`d or `import`ed as a
module, submodule, or dependency - which means `exported` is identical to the
`module.exports` returned by this method.If `exported` did not come from the `exports` of a `module` in `require.cache`,
then this method returns `null`.## License
ISC © Contributors
[opensourceregistry_package_id]: # (458260416784685e5ef3091fee54001785dd4360406aa3000315ff256eef6878)