parent-module
Get the path of the parent module
Last updated a year ago by sindresorhus .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install parent-module 
SYNC missed versions from official npm registry.

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

npm install parent-module

Usage

// bar.js
import parentModule from 'parent-module';

export default function bar() {
	console.log(parentModule());
	//=> '/Users/sindresorhus/dev/unicorn/foo.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.

Tip

Combine it with read-package-up to read the package.json of the parent module.

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', …}
}

Current Tags

  • 3.2.0                                ...           latest (a year ago)

7 Versions

  • 3.2.0                                ...           a year ago
  • 3.1.0                                ...           3 years ago
  • 3.0.0                                ...           5 years ago
  • 2.0.0                                ...           8 years ago
  • 1.0.1                                ...           8 years ago
  • 1.0.0                                ...           8 years ago
  • 0.1.0                                ...           11 years ago
Maintainers (1)
Downloads
Total 22
Today 0
This Week 0
This Month 7
Last Day 0
Last Week 1
Last Month 7
Dependencies (1)
Dev Dependencies (4)
Dependents (2)

© 2010 - cnpmjs.org x YWFE | Home | YWFE