Search code examples
javascriptnode.jstypescriptdirectoryfs

Nodejs get subfolders list


I need to get for specific path of all the subfolders, I've tried with the following

p = '/Users/i099566/projects/mp'

and under mp folder there is sub-folders/folders, which I want to get (not files)

When use the following code I got null, any idea what am I missing here?

const { readdirSync, statSync } = require('fs')
const { join } = require('path')

const dirs = (p: any) => readdirSync(p).filter((f: any) => statSync(join(p, f)).isDirectory())
console.log(dirs)
    

Solution

  • Note that dirs is a function that takes a parameter of type any that is called p.
    With console.log(dirs) you are printing the function and do not call it in any way.

    If you remove (p: any) => and define the variable p before it should work as then dirs stores the results of the expressions of the right side instead of a function.