18

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' 
4
  • 1
    The code you've shown doesn't produce that error. Commented Jan 23, 2019 at 6:26
  • It does. It wont write to a directory not to a folder. Commented Jan 23, 2019 at 6:29
  • 2
    P.S: The result variable has the value [object object] So that is why file name is different. Commented Jan 23, 2019 at 6: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. Commented Jan 23, 2019 at 7:08

4 Answers 4

26

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.

Sign up to request clarification or add additional context in comments.

5 Comments

What is the most recommended way to do this ?
Just put the . before /niktoResult and it will work!
Its always recommended to use relative paths instead of absolute paths!
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'))
├── 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"
10

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'); }) } }) 

2 Comments

is there a cleaner way to do this?
You can use fs.writeFileSync
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") 

Comments

0
if (!req.files || Object.keys(req.files).length === 0) { console.log('No files uploaded'); return { status: "fail", data: "No files uploaded" }; } const uploadedFiles = req.files['files']; const __dirname = path.resolve(); const filesArray = Array.isArray(uploadedFiles) ? uploadedFiles : [uploadedFiles]; const uploadsData = []; for (const file of filesArray) { const currentTimestamp = Date.now(); const uploadPath = path.join(__dirname, '/uploadFile/', `${currentTimestamp}_${file.name}`); await file.mv(uploadPath); const relativeFilePath = `/uploadFile/${currentTimestamp}_${file.name}`; const upload = await prisma.files.create({ data: { userId: parseInt(req.headers.user_id), files: relativeFilePath, }, }); uploadsData.push(upload); } 

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.