I tried to find similar issues but couldn't, sorry if it is already reported.
With a+ flag file should be created if it does not exist. It works with readFileSync, but fails with readFile
There is also similar problem with w+ flag and maybe others, but I did not tested all of them.
const fs = require('fs');
const content = fs.readFileSync('./nonexistingfile1', {
encoding: 'utf-8',
flag: 'a+',
});
console.log(content); // content is empty string, so file was created
fs.readFile(
'./nonexistingfile2',
{ encoding: 'utf-8', flag: 'a+' },
(err, data) => {
console.log(err, data);
// [Error: ENOENT: no such file or directory, open './nonexistingfile2'] {
// errno: -2,
// code: 'ENOENT',
// syscall: 'open',
// path: './nonexistingfile2'
// }
}
);
Always with node v14.11
Works fine in node v12.18.4
File is created before reading and there is no ENOENT error
ENOENT error
[Error: ENOENT: no such file or directory, open './nonexistingfile2'] {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: './nonexistingfile2'
}
I am not sure, since I'm new to Node source codebase, but it might be connected to this PR https://github.com/nodejs/node/pull/27044/files#diff-9a205ef7ee967ee32efee02e58b3482dL314
Possible typo since there is no such thing as options.flags only options.flag.
I have no idea how to test it yet, but I can make PR if this is the problem
@O4epegb a PR would be great. This seems to be a typo.
Ok, I'll make it soon!
This issue made me realize that we have a big inconsistency regarding this option: all methods expect it to be flag in the options object, except the stream ones (createReadStream, createWriteStream), which look for the flags property...
I guess this issue could be closed now?
Fixed here: https://github.com/nodejs/node/pull/35292