fs.statSync throws an error when contained in a function?

I'm making a function that returns a boolean for when a file exists or not, using fs.statSync. It looks like this:

function doesExist (cb) {
  let exists
  try {
    fs.statSync('./cmds/' + firstInitial + '.json')
    exists = true
  } catch (err) {
    exists = err && err.code === 'ENOENT' ? false : true
  }
  cb(exists)
}

Example use case:

let fileExists
doesExist('somefile.json', function (exists) {
  fileExists = exists
})

However, running the code throws me a TypeError: string is not a function. I have no idea why.

I think you want to remove that callback, and add the file name to your parameters:

function doesExist(firstInitial) {
  try {
    fs.statSync('./cmds/' + firstInitial + '.json')
    return true
  } catch(err) {
    return !(err && err.code === 'ENOENT');
  }
}

let fileExists = doesExist('somefile');

Btw, there is also fs.exists.