0

I am using this block of code to create and write a new directory and a file. I just started to learn nodejs

var lib = {};

lib.baseDir = path.join(__dirname,'/../.data/');

lib.create = function(dir,file,data,callback){

fs.open(lib.baseDir+dir+'/'+file+'.json', 'wx', function(err, fileDescriptor){

if(!err && fileDescriptor){

  var stringData = JSON.stringify(data);

  // Write to file and close it
  fs.writeFile(fileDescriptor, stringData,function(err){
    if(!err){
      fs.close(fileDescriptor,function(err){
        if(!err){
          callback(false);
        } else {
          callback('Error closing new file');
        }
      });
    } else {
      callback('Error writing to new file'+'lib.baseDir');
    }
  });
} else {
  callback(err);
  }
 });
};

but I am repeatedly getting the error

{ Error: ENOENT: no such file or directory, open 'C:\Users\Jawahr\Documents\GitHub\node-api\.data\test\newFile.json'
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: 'C:\\Users\\Jawahr\\Documents\\GitHub\\node- 
api\\.data\\test\\newFile.json' }

calling this library in a index.js as

var _data = require('./lib/data');

_data.create('test','newFile', {"asdksadlk" : "asldj"} ,function(err) {
  console.log('this was the error ',err);
});

I've been stuck here for a while, is it because the pathname and filename contained the part "C:" which has colon a reserved char in windows 10, if it is the problem how to solve this.

using windows 10 and NodeJs 8.6.

pavithra rox
  • 1,056
  • 3
  • 11
  • 33
jawakar
  • 1
  • 1
  • 1
  • 1
    is exists directory 'C:\Users\Jawahr\Documents\GitHub\node-api\.data\test\' on your PC? – Vasyl Moskalov Jul 18 '18 at 04:42
  • Possible duplicate of [Create Directory When Writing To File In Node.js](https://stackoverflow.com/questions/13542667/create-directory-when-writing-to-file-in-node-js) – Vasyl Moskalov Jul 18 '18 at 11:13

3 Answers3

0

Can you try this -

fs.open(lib.baseDir+dir+'/'+file+'.json', 'w', function(err, fileDescriptor){

It looks like 'wx' throws an error if file exists -

'w' - Open file for writing. The file is created (if it does not exist) or truncated (if it exists).

'wx' - Like 'w' but fails if the path exists.

'w+' - Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).

'wx+' - Like 'w+' but fails if the path exists.

Referred from here

eduPeeth
  • 1,840
  • 13
  • 21
  • Nope. fs.open(...) throws errors when path to new file is not exists. – Vasyl Moskalov Jul 18 '18 at 08:53
  • check the existence and access permission at the directory `C:\Users\Jawahr\Documents\GitHub\node-api\.data\test` – eduPeeth Jul 18 '18 at 08:55
  • but node fs.open() will automatically create a file and dir when there is no file and dir in that name , sorry if i was wrong correct me – jawakar Jul 18 '18 at 09:45
  • fs.open() create the file if it doesn't exists but it wont create the directory. Try in some existing directory without creating file explicitely. – eduPeeth Jul 18 '18 at 10:02
0

Looks like your put non-existing or non-accessible path to your file. Look:

fs.open('/path/is/not/exists/xx.js','wx',(err,fd)=>{
    if (err) {
      console.log(err.message);
    }
});

and got

 Error: ENOENT: no such file or directory, open '/path/is/not/exists/xx.js'

In case when file is already exists you will got something like Error: EEXIST: file already exists, open '...'

And last, but not least. Instead of lib.baseDir+dir+'/'+file+'.json' better solution is use path.join(lib.baseDir,dir,file+'.json') from path module

Vasyl Moskalov
  • 4,242
  • 3
  • 20
  • 28
0

Add a check for directory or create before fs.open

if (!fs.existsSync(dir)){
    fs.mkdirSync(dir);
}

Then the rest of your code will work. as fs.open only creates file if it doesn't exist, it does not create directory

Pseudyx
  • 63
  • 6