0

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")
}

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer, I updated the question, I still get the error even when it creates the file. I'm a newbie so I'm not sure what I'm doing wrong there. (I don't use semicolons since the project's using webpack)
The callback will be called when it is successful, and when there is an error. The documentation says that fs.writeFile Asynchronously writes data to a file, replacing the file if it already exists. So in case one when it exists it replaces it and then calls callback with error as null. It will alert 'created'. When the file doesn't exist, it does the same thing but only creates the file rather than replacing it. You aren't doing anything wrong other than using alert when you don't need to. You should alert(error) if you really want to know if there is an error.
Ahh, got it, thank you for explaining it and helping out. I managed to make it work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.