What is parents?
The 'parents' npm package is a utility that allows you to get an array of the directory paths from a given directory up to the root directory. It is useful for file system navigation and operations that require knowledge of a file or directory's ancestry.
What are parents's main functionalities?
Get parent directories
This feature allows you to retrieve an array of parent directories of a given path. The array starts with the immediate parent directory and ends with the root directory.
const parents = require('parents');
const dirs = parents('/usr/local/bin/node');
console.log(dirs); // ['/usr/local/bin', '/usr/local', '/usr', '/']
Other packages similar to parents
path-parents
The 'path-parents' package provides similar functionality to 'parents' by returning an array of parent paths. It differs in its API and additional options for filtering the results.
find-parent-dir
The 'find-parent-dir' package finds the nearest parent directory that contains a given file or directory. It is similar to 'parents' but focuses on locating a specific file or directory in the ancestry rather than listing all parent directories.
pkg-dir
The 'pkg-dir' package finds the root directory of a Node.js project or npm package. This is a more specialized use case compared to 'parents', which is more general in listing parent directories.
parents
Return all the parent directories of a directory, inclusive of that directory.
example
dirname
var parents = require('parents');
var dirs = parents(__dirname);
console.dir(dirs);
[ '/home/substack/projects/node-parents/example',
'/home/substack/projects/node-parents',
'/home/substack/projects',
'/home/substack',
'/home',
'/' ]
win32
var parents = require('parents');
var dir = 'C:\\Program Files\\Maxis\\Sim City 2000\\cities';
var dirs = parents(dir, { platform : 'win32' });
console.dir(dirs);
[ 'C:\\Program Files\\Maxis\\Sim City 2000\\cities',
'C:\\Program Files\\Maxis\\Sim City 2000',
'C:\\Program Files\\Maxis',
'C:\\Program Files',
'C:' ]
methods
var parents = require('parents')
parents(dir, opts)
Return an array of the parent directories of dir
, including and starting with
dir
. If a dir
isn't specified, process.cwd()
will be used.
Optionally specify an opts.platform
to control whether the separator and paths
works the unixy way with '/'
or the windowsy way where sometimes things use
'/'
and sometimes they use '\\'
and also there are leading drive letters and
other exotic features. If opts.platform
isn't specified, process.platform
will be used. Anything that matches /^win/
will use the windowsy behavior.
install
With npm do:
npm install parents
licence
MIT