https://github.com/sindresorhus/parent-module
Get the path of the parent module
https://github.com/sindresorhus/parent-module
Last synced: 7 months ago
JSON representation
Get the path of the parent module
- Host: GitHub
- URL: https://github.com/sindresorhus/parent-module
- Owner: sindresorhus
- License: mit
- Created: 2016-01-22T14:58:35.000Z (almost 10 years ago)
- Default Branch: main
- Last Pushed: 2023-11-04T09:57:21.000Z (about 2 years ago)
- Last Synced: 2025-04-11T03:06:30.305Z (7 months ago)
- Language: JavaScript
- Size: 20.5 KB
- Stars: 77
- Watchers: 4
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- Funding: .github/funding.yml
- License: license
- Security: .github/security.md
Awesome Lists containing this project
- awesome-nodejs-precise - parent-module - module .svg?style=social&label=Star&maxAge=2592000?style=flat-square)]() | Get the path of the parent module. | (Packages / Miscellaneous)
- awesome-nodejs-cn - parent-module - Get the path of the parent module. (Number / Miscellaneous)
README
# parent-module
> Get the path of the parent module
Node.js exposes `module.parent`, but it only gives you the first cached parent, which is not necessarily the actual parent.
## Install
```sh
npm install parent-module
```
## Usage
```js
// bar.js
import parentModule from 'parent-module';
export default function bar() {
console.log(parentModule());
//=> '/Users/sindresorhus/dev/unicorn/foo.js'
};
```
```js
// foo.js
import bar from './bar.js';
bar();
```
## API
### parentModule(filePath?)
By default, it will return the path of the immediate parent.
#### filePath
Type: `string`\
Default: [`__filename`](https://nodejs.org/api/globals.html#globals_filename)
The file path of the module of which to get the parent path.
Useful if you want it to work [multiple module levels down](fixtures/filepath).
## Tip
Combine it with [`read-package-up`](https://github.com/sindresorhus/read-package-up) to read the package.json of the parent module.
```js
import path from 'node:path';
import {readPackageUpSync} from 'read-package-up';
import parentModule from 'parent-module';
console.log(readPackageUpSync({cwd: path.dirname(parentModule())}).pkg);
//=> {name: 'chalk', version: '1.0.0', …}
```