Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
find-parent-dir
Advanced tools
Finds the first parent directory that contains a given file or directory.
The find-parent-dir npm package is a utility for finding the parent directory of a given directory that contains a specific file or directory. It is useful for tasks such as locating configuration files or project root directories in a file system hierarchy.
Find Parent Directory Containing a Specific File
This feature allows you to find the parent directory that contains a specific file, such as 'package.json'. The code sample demonstrates how to use the findParentDir function to locate the parent directory of the current directory (__dirname) that contains 'package.json'.
const findParentDir = require('find-parent-dir');
findParentDir(__dirname, 'package.json', (err, dir) => {
if (err) {
console.error(err);
} else {
console.log('Parent directory containing package.json:', dir);
}
});
Find Parent Directory Containing a Specific Directory
This feature allows you to find the parent directory that contains a specific directory, such as 'node_modules'. The code sample demonstrates how to use the findParentDir function to locate the parent directory of the current directory (__dirname) that contains 'node_modules'.
const findParentDir = require('find-parent-dir');
findParentDir(__dirname, 'node_modules', (err, dir) => {
if (err) {
console.error(err);
} else {
console.log('Parent directory containing node_modules:', dir);
}
});
The find-up package is a utility for finding a file or directory by walking up parent directories. It is similar to find-parent-dir but offers more flexibility by allowing you to search for files or directories with a specific name or pattern. It also supports promises and synchronous operations.
The locate-path package is used to find the first instance of a file or directory in a list of paths. It is similar to find-parent-dir in that it helps locate files or directories, but it focuses on searching through a list of paths rather than walking up the directory tree.
The pkg-up package is a specialized utility for finding the closest package.json file by walking up parent directories. It is similar to find-parent-dir but is specifically designed for locating package.json files, making it a more focused tool for Node.js projects.
Finds the first parent directory that contains a given file or directory.
npm install find-parent-dir
// assuming this is called from a file in a subdirectory of /myprojects/foo which contains .git directory
var findParentDir = require('find-parent-dir');
findParentDir(__dirname, '.git', function (err, dir) {
// has err if some file access error occurred
console.log(dir); // => /myprojects/foo/
// if parent dir wasn't found, dir is null
})
// Same using `sync` method
var dir;
try {
dir = findParentDir.sync(__dirname, '.git');
console.log(dir); // => /myprojects/foo/
// if parent dir wasn't found, dir is null
} catch(err) {
console.error('error', err);
}
FAQs
Finds the first parent directory that contains a given file or directory.
We found that find-parent-dir 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.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.