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