Q:

Getting errors trying to create a new file using different methods with node fs in an Electron app

I'm trying to create a new file, but both of the methods below throw me an error:
method 1:
fs.writeFile(fullPath, '', (error) => { alert("exist") })

if a file with the specified name exist: console throws exist
if a file with the specified name doesn't exist: console throws exist AND creates the file

method 2:
if (!fs.existsSync(fullPath)) { 
  fs.appendFile(fullPath)
} else {     
  alert("exist")
}

if a file with the specified name exist: console throws exist
if a file with the specified name doesn't exist: it creates the file AND gives me this error  DeprecationWarning: Calling an asynchronous function without callback is deprecated.

Question:
What am I doing wrong?

Update:
I also tried this method suggested in the answer below:
// fullPath= 'C:/Users/Name/test.txt'

fs.writeFile(fullPath, '', (error) => {
  if(error) {
    alert("exist")
    return
  }
    alert("created")
})

and I get this:

if a file with the specified name exist: console throws created
if a file with the specified name doesn't exist: console throws created AND creates the file

Update 2:
I managed to make it work in a slightly different way:
if (!fs.existsSync(fullPath)) { 
  fs.writeFileSync(fullPath, '')
} else {
  alert("exist")
}

A:

For method 1, you are using fs.writeFile(file, data[, options], callback). So that callback will be called no matter what, alerting 'exists'. You should have a check, something like:
fs.writeFile(fullPath, '', (error) => {
  if(error) {
    alert("exist");
    return;
  }

  // no error, do what you want.
});

Reference: https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback
For method 2, you are getting the warning because you are calling fs.appendFile without a callback. Either use fs.appendFileSync or give it a callback.
Reference: https://nodejs.org/api/fs.html#fs_fs_appendfile_file_data_options_callback

