16

I am writing some text to a file us the fs module.

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

Now this works fine. I want to write this file to a niktoResults folder in my project but when i do

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

It results an error. I don't know how to define the directory path that will help me overcome this.

Error:
Error: ENOENT: no such file or directory, open '/niktoResults/[object Object].txt'
Danyal Ahmad
  • 163
  • 1
  • 1
  • 6
  • 1
    The code you've shown doesn't produce that error. – Jonas Wilms Jan 23 '19 at 06:26
  • It does. It wont write to a directory not to a folder. – Danyal Ahmad Jan 23 '19 at 06:29
  • 2
    P.S: The result variable has the value [object object] So that is why file name is different. – Danyal Ahmad Jan 23 '19 at 06:30
  • 1
    Your code in your question does not show any `results` variable. It appears that you actual code is different than what you show in the question because the code in your question would not create the error that you see. Please show us the ACTUAL code that causes that error. Only then can people help you. – jfriend00 Jan 23 '19 at 07:08

3 Answers3

24

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.

Lucifer
  • 645
  • 6
  • 14
  • What is the most recommended way to do this ? – Danyal Ahmad Jan 23 '19 at 06:31
  • Just put the . before /niktoResult and it will work! – Lucifer Jan 23 '19 at 06:36
  • Its always recommended to use relative paths instead of absolute paths! – Lucifer Jan 23 '19 at 06:37
  • And you can access to the file : localhost:3002/niktoResults/result.txt . but you had to add app.use('/niktoResults', express.static(process.cwd() + '/niktoResults')) – Anas Zayene Dec 23 '21 at 15:06
  • ├── package-lock.json ├── package.json └── src ├── app-with-argv.js ├── app.js ├── salida │ ├── tabla-4.txt │ ├── tabla-5.txt │ └── tabla-6.txt ├── tablas │ └── tabla-2.txt ├── utils │ └── multiplicar.js └── yargs-config └── optionsYarg.js I have this repo, i want to save files in the "salida" folder but i cant do it writeFilesync("../src/salida/") doesnt work writeFylesync("./salida") doesnt work either The file that call the writeFileSync is "app.js" – Programmer89 Apr 04 '22 at 21:38
9

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');
     })
  }
})
elraphty
  • 630
  • 6
  • 13
7

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")
Ivan Shatsky
  • 13,267
  • 2
  • 21
  • 37
Samuel Chibuike
  • 146
  • 2
  • 4