# parent-module

> Get the path of the parent module

Latest version **3.2.0** (published 2025-09-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install parent-module
pnpm add parent-module
yarn add parent-module
bun add parent-module
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 3.2.0 |
| Published | 2025-09-15 |
| First published | 2016-01-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | ^12.20.0 \|\| ^14.13.1 \|\| >=16.0.0 |
| Dependencies | 1 |
| Unpacked size | 4.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 78 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | parent, module, package, caller, calling, module, path, callsites, callsite, stacktrace, stack, trace, function, file |

## Links

- npm: https://www.npmjs.com/package/parent-module
- Repository: https://github.com/sindresorhus/parent-module
- Homepage: https://github.com/sindresorhus/parent-module#readme
- Issues: https://github.com/sindresorhus/parent-module/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/parent-module

## Dependencies (1)

- [callsites](https://npm.io/package/callsites.md) ^4.1.0

## Alternatives

- [base64url](https://npm.io/package/base64url.md) — 6.1M weekly downloads
- [get-installed-path](https://npm.io/package/get-installed-path.md) — 502.9K weekly downloads
- [@uppy/url](https://npm.io/package/@uppy/url.md) — 185.8K weekly downloads
- [@d3fc/d3fc-shape](https://npm.io/package/@d3fc/d3fc-shape.md) — 16.2K weekly downloads
- [localizer](https://npm.io/package/localizer.md) — 226 weekly downloads

## Recent versions

- 3.2.0 (latest) — 2025-09-15
- 3.1.0 — 2023-09-14
- 3.0.0 — 2021-10-04
- 2.0.0 — 2019-04-30
- 1.0.1 — 2019-03-28
- 1.0.0 — 2018-12-25
- 0.1.0 — 2016-01-22

## README

# parent-module

> Get the path of the parent module

This module provides a reliable way to get the file path of the module that called your code, working correctly with both CommonJS and ESM modules.

## 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?)

Returns: `string | undefined`

Returns the file path of the immediate parent module, or `undefined` if there is no parent module (for example, when called from the top-level of an entry module).

#### filePath

Type: `string`\
Default: `import.meta.filename`

The file path of the module for which to get the parent path.

Useful for getting the parent of a specific module when the call traverses [multiple module levels](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';

const parent = parentModule();
if (parent) {
	console.log(readPackageUpSync({cwd: path.dirname(parent)}).pkg);
	//=> {name: 'chalk', version: '1.0.0', …}
}
```

---
_Source: https://npm.io/package/parent-module · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
