0
# Parent Module
1
2
Parent Module provides a simple utility to get the path of the parent module that called the current module. It leverages the callsites library to analyze the JavaScript call stack and identify the calling module's location, making it useful for debugging, logging, and dynamic module resolution.
3
4
## Package Information
5
6
- **Package Name**: parent-module
7
- **Package Type**: npm
8
- **Language**: JavaScript with TypeScript definitions
9
- **Installation**: `npm install parent-module`
10
11
## Core Imports
12
13
```javascript
14
import parentModule from 'parent-module';
15
```
16
17
## Basic Usage
18
19
```javascript
20
// bar.js
21
import parentModule from 'parent-module';
22
23
export default function bar() {
24
console.log(parentModule());
25
//=> '/Users/sindresorhus/dev/unicorn/foo.js'
26
}
27
28
// foo.js
29
import bar from './bar.js';
30
31
bar();
32
```
33
34
## Capabilities
35
36
### Parent Module Detection
37
38
Gets the path of the parent module that called the current module, with support for both immediate parent detection and multi-level resolution.
39
40
```javascript { .api }
41
/**
42
* Get the path of the parent module
43
* @param filePath - The file path of the module of which to get the parent path
44
* @returns The file path of the parent module, or undefined if no parent can be determined
45
*/
46
function parentModule(filePath?: string): string | undefined;
47
```
48
49
**Parameters:**
50
- `filePath` (optional): `string` - The file path of the module of which to get the parent path. When provided, returns the parent of the module that called the specified file path. When omitted, returns the immediate parent module.
51
52
**Returns:**
53
- `string | undefined` - The file path of the parent module, or `undefined` if no parent can be determined
54
55
**Behavior:**
56
- Without `filePath`: Returns the path of the immediate parent module (2nd level up in call stack)
57
- With `filePath`: Traverses the call stack to find the parent of the module that called the specified file path
58
- Automatically skips native modules like 'module.js'
59
- Handles call stack analysis to determine the appropriate calling context
60
61
**Usage Examples:**
62
63
```javascript
64
// Example 1: Basic usage (immediate parent)
65
// In module-b.js
66
import parentModule from 'parent-module';
67
68
export function logCaller() {
69
console.log(parentModule());
70
// When called from module-a.js, outputs: '/path/to/module-a.js'
71
}
72
73
// Example 2: Multi-level parent resolution
74
// In deeply-nested.js
75
import parentModule from 'parent-module';
76
77
export function findSpecificParent() {
78
// Find the parent of the module that called 'intermediate.js'
79
const parent = parentModule('/path/to/intermediate.js');
80
console.log(parent);
81
// Outputs the path of the module that called intermediate.js
82
}
83
84
// Example 3: Integration with package.json reading
85
import path from 'node:path';
86
import {readPackageUpSync} from 'read-pkg-up';
87
import parentModule from 'parent-module';
88
89
// Get package.json of the calling module
90
const parentPkg = readPackageUpSync({
91
cwd: path.dirname(parentModule())
92
}).pkg;
93
console.log(parentPkg.name); // Name of the calling module's package
94
```
95
96
## Node.js Compatibility
97
98
- **Supported Versions**: Node.js ^12.20.0 || ^14.13.1 || >=16.0.0
99
- **Module Type**: ES6 module only
100
- **Dependencies**: callsites@^4.1.0
101
102
## TypeScript Support
103
104
Full TypeScript definitions are included via `index.d.ts`, providing complete type safety and IntelliSense support.
105
106
```typescript
107
import parentModule from 'parent-module';
108
109
// TypeScript knows the return type is string | undefined
110
const parent: string | undefined = parentModule();
111
const specificParent: string | undefined = parentModule('/path/to/file.js');
112
```