Home>

On an attempt to determine if the passed path is a folderif (await fs.stat (path) .isDirectory ()) {path += '/index.html'; }the node throws an error: TypeError: fs.stat (...). isDirectory is not a function

Can you read the documentation?

Alexey Ten2021-12-16 19:27:10

Why is it outdated? nodejs.org/dist/latest-v16.x/docs/api/fs.html#statsisdirectory

Alexey Ten2021-12-15 19:32:28

how do you import fs? Which version of .stat are you calling?

Grundy2021-12-16 03:59:49
  • Answer # 1

    FunctionisDirectoryis not obsolete.

    In this case, the method is used incorrectlystat...

    Assuming the.

    This method returnsPromiseto wait withawait... In this case

    await fs.stat (path) .isDirectory ()
    

    awaitapplies to the whole expressionfs.stat (path) .isDirectory (), henceisDirectorycalled atPromise... There is no such method, which is why the error occurs.

    To fix it, just place the parentheses:

    (await fs.stat (path)). isDirectory ()
    
  • Answer # 2

    FunctionisDirectoryis not obsolete.

    In this case, the method is used incorrectlystat...

    Assuming the.

    This method returnsPromiseto wait withawait... In this case

    await fs.stat (path) .isDirectory ()
    

    awaitapplies to the whole expressionfs.stat (path) .isDirectory (), henceisDirectorycalled atPromise... There is no such method, which is why the error occurs.

    To fix it, just place the parentheses:

    (await fs.stat (path)). isDirectory ()
    
  • Answer # 3

    FunctionisDirectoryis not obsolete.

    In this case, the method is used incorrectlystat...

    Assuming the.

    This method returnsPromiseto wait withawait... In this case

    await fs.stat (path) .isDirectory ()
    

    awaitapplies to the whole expressionfs.stat (path) .isDirectory (), henceisDirectorycalled atPromise... There is no such method, which is why the error occurs.

    To fix it, just place the parentheses:

    (await fs.stat (path)). isDirectory ()