Name | Date | Size | #Lines | LOC | ||
---|---|---|---|---|---|---|
.. | 22-Nov-2021 | - | ||||
license | H A D | 22-Nov-2021 | 1.1 KiB | 10 | 5 | |
package.json | H A D | 22-Nov-2021 | 712 | 47 | 46 | |
readme.md | H A D | 22-Nov-2021 | 1.4 KiB | 68 | 40 |
readme.md
1# parent-module [![Build Status](https://travis-ci.org/sindresorhus/parent-module.svg?branch=master)](https://travis-ci.org/sindresorhus/parent-module) 2 3> Get the path of the parent module 4 5Node.js exposes `module.parent`, but it only gives you the first cached parent, which is not necessarily the actual parent. 6 7 8## Install 9 10``` 11$ npm install parent-module 12``` 13 14 15## Usage 16 17```js 18// bar.js 19const parentModule = require('parent-module'); 20 21module.exports = () => { 22 console.log(parentModule()); 23 //=> '/Users/sindresorhus/dev/unicorn/foo.js' 24}; 25``` 26 27```js 28// foo.js 29const bar = require('./bar'); 30 31bar(); 32``` 33 34 35## API 36 37### parentModule([filepath]) 38 39By default, it will return the path of the immediate parent. 40 41#### filepath 42 43Type: `string`<br> 44Default: [`__filename`](https://nodejs.org/api/globals.html#globals_filename) 45 46Filepath of the module of which to get the parent path. 47 48Useful if you want it to work [multiple module levels down](https://github.com/sindresorhus/parent-module/tree/master/fixtures/filepath). 49 50 51## Tip 52 53Combine it with [`read-pkg-up`](https://github.com/sindresorhus/read-pkg-up) to read the package.json of the parent module. 54 55```js 56const path = require('path'); 57const readPkgUp = require('read-pkg-up'); 58const parentModule = require('parent-module'); 59 60console.log(readPkgUp.sync({cwd: path.dirname(parentModule())}).pkg); 61//=> {name: 'chalk', version: '1.0.0', …} 62``` 63 64 65## License 66 67MIT © [Sindre Sorhus](https://sindresorhus.com) 68