fs.statSync is not a function

Viewed 5075

Node version: 10.16.3

I'm so confused. Apparently fs.statSync in the following code is not a function... Can somebody please explain to me why the following code is throwing this error?

fs.readFile('./config.json', 'utf8',
  (error, config) => {
    console.log(`1. ${config}`)
    if (error) console.error(`Error: ${error}`)
    else {
      for (const archive of JSON.parse(config).archives){
        console.log(`2. ${archive}`)
        console.log(`3. ${fs.statSync(archive).isDirectory()}`)
      }
    }
  }
)

Console

1. {
  "archives": [
    "C:\\Windows",
    "C:\\AMD",
    "C:\\MSOCache",
    "C:\\PerfLogs",
    "C:\\Program Files",
    "C:\\Program Files (x86)",
    "C:\\ProgramData",
    "C:\\Users",
    "C:\\WebDrivers"
  ]
}

2. C:\Windows

3. Uncaught TypeError: fs.statSync is not a function
    at file-system.js:128
    at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:61)
1 Answers

Looks like your promisifying fs. It won't promisify the synchronous methods.

You rarely want to use the Sync methods anyhow, they'll clog up your event loop.

Related