
Security News
Axios Maintainer Confirms Social Engineering Attack Behind npm Compromise
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.
list-subdirectories
Advanced tools
A simple function that allows you to pass in a path to a directory and it will return a string array of subdirectories.
yarn add list-subdirectories
or
npm install list-subdirectories
The following code will log out a string array of subdirectories for the current directory:
const listSubdirectories = require('list-subdirectories')
listSubdirectories('.').then(list => console.log(list))
Notice that it doesn't support callbacks and is promisified by default.
This is the only method available in the library. It is the default export (there are no named exports.)
It expects 1 required parameter. That parameter can be a string or an object.
If that parameter is a string:
It uses the parameter as the absolute path to scan for subdirectories within. It assumes the defaults for the other options:
{
filter: undefined,
recursive: false,
maxDepth: 1,
}
I.e. it will scan for all subdirectories (unfiltered) at the 1st level only (no subdirectories of subdirectories.)
If the 1st parameter is an object:
The options object definition looks like this:
{
path: string, // path is required and must be a string
filter?: string, // filter is an optional string value
recursive?: boolean, // recursive is an optional boolean value
maxDepth?: number, // maxDepth is an optional number and can only be present if recursive is set to true
}
If the optional values are not provided, they will default to:
{
filter: undefined, // no filter set, so it will return all subdirectories
recursive: false, // don't scan recursively so it will only return immediate subdirectories
maxDepth: undefined, // no max depth defined as we aren't scanning recursively
}
More details on the possible options:
If the path passed in is not found, the System Error: ENOENT: no such file or directory error is thrown:
{ Error: ENOENT: no such file or directory, scandir '/[FULLPATH]/[DIRECTORYTHATDOESNOTEXIST]'
errno: -2,
code: 'ENOENT',
syscall: 'scandir',
path: '/[FULLPATH]/[DIRECTORYTHATDOESNOTEXIST]' }
If arguments supplied are of the wrong type, then a TypeError is thrown:
{ Error: TypeError: ...
stack: ...
}
If both maxDepth is set and recursive is not set to true, then a RangeError is thrown:
{ Error: RangeError: You can only set maxDepth if recursive is set to true,
stack: ...
}
If you specify zero, a negative number or a floating point number as a level, then a RangeError is thrown:
{ Error: RangeError: levels must be a non-negative non-zero integer,
stack: ...
}
This will return a list of subdirectories (non-recursive) of the current directory:
listSubdirectories('.').then(list => console.log(list))
This will return a list of subdirectories (non-recursive) of the current directory that start with the string foo:
listSubdirectories('.', { filter: '^foo.*' }).then(list => console.log(list))
This will return a list of all subdirectories recursively (no depth limit):
listSubdirectories('.', { recursive: true }).then(list => console.log(list))
This will return a list of all subdirectories recursively to a limit of 2 (i.e. subdirectories and THEIR subdirectories, but no deeper):
listSubdirectories('.', { recursive: true, maxDepth: 2 }).then(list => console.log(list))
All of the following will throw a RangeError:
listSubdirectories('.', { maxDepth: 2, recursive: false }).then(list => console.log(list))
listSubdirectories('.', { maxDepth: -1.5 }).then(list => console.log(list))
All of the following will throw a TypeError:
listSubdirectories(0).then(list => console.log(list))
listSubdirectories('.', '').then(list => console.log(list))
listSubdirectories('.', { filter: true }).then(list => console.log(list))
listSubdirectories('.', { maxDepth: 'something' }).then(list => console.log(list))
listSubdirectories('.', { recursive: 'do it please' }).then(list => console.log(list))
This will throw a More arguments needed Error:
listSubdirectories().then(list => console.log(list))
ISC (i.e. feel free to use this for any purpose, but I accept no liability for it's use.)
Feel free to open issues or even better submit pull requests.
yarn lint.)yarn test.)yarn test.)If you would like to become a maintainer, feel free to contact me. You would probably have to have become known to me via submitted pull requests first.
FAQs
returns an array of subdirectory path strings
We found that list-subdirectories 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
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.