
Research
Security News
The Growing Risk of Malicious Browser Extensions
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
parent-module
Advanced tools
The parent-module npm package allows you to find and require the parent module of a current module, which is useful in situations where you need to interact with or modify the behavior of the module that required your module. This can be particularly useful for plugins or modules that extend or modify the functionality of other modules.
Find and require the parent module
This feature allows you to find and require the parent module of the current module. The code sample demonstrates how to require the parent-module package and then use it to get the parent module. The 'parent' variable will contain the module object of the parent, and you can access properties such as 'filename' to get the file name of the parent module.
const parentModule = require('parent-module');
const parent = parentModule();
console.log(parent.filename);
The caller package allows you to get the file path of the caller function. It is similar to parent-module in that it helps you identify the calling module, but it focuses on the function call stack rather than the module hierarchy.
The caller-path package is similar to caller, providing the path of the caller function. It is a more focused tool compared to parent-module, which provides the entire parent module object.
This package is used to always get the main entry point file path of a module, regardless of the environment (e.g., when using a module bundler). It differs from parent-module by focusing on the entry point file rather than the immediate parent module.
Get the path of the parent module
Node.js exposes module.parent
, but it only gives you the first cached parent, which is not necessarily the actual parent.
npm install parent-module
// 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();
By default, it will return the path of the immediate parent.
Type: string
Default: __filename
The file path of the module of which to get the parent path.
Useful if you want it to work multiple module levels down.
Combine it with read-pkg-up
to read the package.json of the parent module.
import path from 'node:path';
import {readPackageUpSync} from 'read-pkg-up';
import parentModule from 'parent-module';
console.log(readPackageUpSync({cwd: path.dirname(parentModule())}).pkg);
//=> {name: 'chalk', version: '1.0.0', …}
FAQs
Get the path of the parent module
The npm package parent-module receives a total of 43,377,943 weekly downloads. As such, parent-module popularity was classified as popular.
We found that parent-module demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
Research
Security News
An in-depth analysis of credential stealers, crypto drainers, cryptojackers, and clipboard hijackers abusing open source package registries to compromise Web3 development environments.
Security News
pnpm 10.12.1 introduces a global virtual store for faster installs and new options for managing dependencies with version catalogs.