Last change
on this file since 6fe77af was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.4 KB
|
Rev | Line | |
---|
[6a3a178] | 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 |
|
---|
| 5 | Node.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
|
---|
| 19 | const parentModule = require('parent-module');
|
---|
| 20 |
|
---|
| 21 | module.exports = () => {
|
---|
| 22 | console.log(parentModule());
|
---|
| 23 | //=> '/Users/sindresorhus/dev/unicorn/foo.js'
|
---|
| 24 | };
|
---|
| 25 | ```
|
---|
| 26 |
|
---|
| 27 | ```js
|
---|
| 28 | // foo.js
|
---|
| 29 | const bar = require('./bar');
|
---|
| 30 |
|
---|
| 31 | bar();
|
---|
| 32 | ```
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | ## API
|
---|
| 36 |
|
---|
| 37 | ### parentModule([filepath])
|
---|
| 38 |
|
---|
| 39 | By default, it will return the path of the immediate parent.
|
---|
| 40 |
|
---|
| 41 | #### filepath
|
---|
| 42 |
|
---|
| 43 | Type: `string`<br>
|
---|
| 44 | Default: [`__filename`](https://nodejs.org/api/globals.html#globals_filename)
|
---|
| 45 |
|
---|
| 46 | Filepath of the module of which to get the parent path.
|
---|
| 47 |
|
---|
| 48 | Useful 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 |
|
---|
| 53 | Combine it with [`read-pkg-up`](https://github.com/sindresorhus/read-pkg-up) to read the package.json of the parent module.
|
---|
| 54 |
|
---|
| 55 | ```js
|
---|
| 56 | const path = require('path');
|
---|
| 57 | const readPkgUp = require('read-pkg-up');
|
---|
| 58 | const parentModule = require('parent-module');
|
---|
| 59 |
|
---|
| 60 | console.log(readPkgUp.sync({cwd: path.dirname(parentModule())}).pkg);
|
---|
| 61 | //=> {name: 'chalk', version: '1.0.0', …}
|
---|
| 62 | ```
|
---|
| 63 |
|
---|
| 64 |
|
---|
| 65 | ## License
|
---|
| 66 |
|
---|
| 67 | MIT © [Sindre Sorhus](https://sindresorhus.com)
|
---|
Note:
See
TracBrowser
for help on using the repository browser.