Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/parent-module
Get the path of the parent module
https://github.com/sindresorhus/parent-module
Last synced: about 1 month 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 9 years ago)
- Default Branch: main
- Last Pushed: 2023-11-04T09:57:21.000Z (about 1 year ago)
- Last Synced: 2024-03-22T08:58:15.660Z (8 months ago)
- Language: JavaScript
- Size: 20.5 KB
- Stars: 75
- Watchers: 5
- Forks: 9
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- Funding: .github/funding.yml
- License: license
- Security: .github/security.md
Awesome Lists containing this project
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', …}
```