'Is there any way to solve NODE createReadSream error ENOENT: no such file or directory with fs.unlink?
Goal
- check file is exists
- if a file exists delete that
Here is stand-alone test code
const fs = require("fs")
const fileName = "a.txt"
fs.writeFile(fileName, "Hello World", function () { })
fs.createReadStream(fileName) // error!
fs.unlink(fileName, function (err) {
if(err) console.error(err, "err")
})
It works on MacOS with node v12.13.0 but not working on MacOS node v16.13.0 and Ubuntu20.04.3 LTS with node v12.13.0, v16.13.0
I know that not a good idea to use createReadStream for files exists but I'd want to know what's wrong with it.
And here is my revised code.
version1. using createReadStream
const file = fs.createReadStream(filePath)
file.on('end', () => {
fs.unlink(filePath, function(err) {
if (err) console.error(err, "err")
})
})
version2. using existsSync
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath)
}
Question
What is the problem, why MacOS v12.13.0 works but MacOS v16.13.0. and Why not working on Ubuntu
Thanks in advance.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
