How to write a file to specific directory in NodeJS?

Do this

const fs = require('fs');
const path = require('path');

let baseDir = path.join(__dirname, '/./niktoResults/');
fs.open(`${baseDir}+result.txt`, 'wx', (err, desc) => {
  if(!err && desc) {
     fs.writeFile(desc, 'sample data', (err) => {
       // Rest of your code
       if (err) throw err;               
       console.log('Results Received');
     })
  }
})

You have to understand that you can give either absolute path or relative path. Currently what you can do is

fs.writeFile('./niktoResults/result.txt', 'This is my text', function (err) {
  if (err) throw err;               console.log('Results Received');
}); 

Here . refers to current directory. Therefore ./niktoResults refers to niktoResults folder in current directory.


This code writes "This is an example text" into result.txt at niktoResults folder:

const fs = require('fs');
const path = require('path');

fs.writeFileSync(path.join(__dirname,"niktoResults","result.txt"), "This is an example text","UTF8")