nodejs / node

Node.js JavaScript runtime ✨🐢🚀✨
https://nodejs.org
Other
108.07k stars 29.83k forks source link

flags in fs.open not working #26511

Closed ashu8912 closed 5 years ago

ashu8912 commented 5 years ago

Version: 10.15.0 Platform: Darwin Ashus-Air.lan 18.2.0 Darwin Kernel Version 18.2.0: Fri Oct 5 19:41:49 PDT 2018; root:xnu-4903.221.2~2/RELEASE_X86_64 x86_64 Subsystem: fs


fs.open(`${lib.baseDir}${dir}/${file}.json`,'wx',function(err,fileDescriptor){
    console.log(err,fileDescriptor);
   //with wx flag the file should be created if its not there and should give error if its not there
   //but even when there is no such file i get the below error,i also get the below error  when i use the "w" flag
   /**getting error{ [Error: ENOENT: no such file or directory, open 
   '/Users/ashughildiyal/Desktop/WebDevelopment/pirpleNode/app/.data/test/sample.json']
    errno: -2,
    code: 'ENOENT',
    syscall: 'open',
     path:
   '/Users/ashughildiyal/Desktop/WebDevelopment/pirpleNode/app/.data/test/sample.json' }*/
    if(!err && fileDescriptor)
    {
      let dataString=JSON.stringify(data);
      fs.writeFile(fileDescriptor,dataString,(err)=>{
          if(!err)
          {
         fs.close(fileDescriptor,(err)=>{
             if(!err)
             {
               callback(false);
             }else{
                 console.log("could not close file")
             }
         })
          }else{
              callback("could not write to the newly created file")
          }
      })
    }else{
        callback("couldn't create file it may already exist")
    }
   })```
bnoordhuis commented 5 years ago

The ENOENT probably means some part of the path (one or more subdirectories) is missing. If you're sure that's not the case, then please post a standalone test case, i.e., something that's ready to run.

ashu8912 commented 5 years ago

when i tried by giving a manual path it does work

but for the dynamic path its not working the dynamic path for me when i console it is /Users/ashughildiyal/Desktop/WebDevelopment/pirpleNode/app/.data/test/sample.json and thats the exact path no subdirectories missing but it does not works for this path

ashu8912 commented 5 years ago

for the baseDir i used path.join(__dirname,"/../.data/") .data is the directory name

bnoordhuis commented 5 years ago

I'm going to close this for lack of a standalone test case. I have no reason to believe there is actually a Node.js bug. Try nodejs/help if you want to pursue this further.

edgewood1 commented 5 years ago

I had the same issue - same error when attempting to create a directory/file within .data; however, when I only attempted to create a file in .data, it worked fine.

ishan123456789 commented 5 years ago

I agree with @edgewood1 I think this wasn't the case with the older versions of nodeJS. Here's a way to replicate running fs.open('../folderThatExists/folderThatDoesNot/fileThatDoesNot.json', 'wx') So if you try fs.open('../folderThatExists/fileThatDoesNot.json', 'wx') it shall work.

alimbolar commented 2 years ago

I am having the same issue.. in the filename argument, if I don't use the ${dir} then it works fine... just wanted to know if this worked for any of you guys and if yes, then how?

I am assuming that using 'wx' should allow creation of directory if it doesn't exist too in the path for the filename provided..