Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
Why is the data not written to the file?

I have this code which is supposed to write data in text file:

const test = [
  'test1',
  'test2'
]
fs.writeFile('file.txt', test, {flag: "a"}, err => {
  if (err) throw err
})

The problem is that code works without errors, but there is no data in file. If I just put and execute this piece of code in some test file, it works correctly, but not in function, where this code is supposed to be. What's wrong? Why there is no data in file if I execute code in function, but everything works correctly, when I execute it in test file?

Here is the code of function:

const fs = require("fs");

(async () => {
    const test = [
      'test1',
      'test2'
    ]
    fs.writeFile('file.txt', test, {flag: "a"}, err => {
      if (err) throw err
    })

    process.exit(1);
  } catch (e) {
    console.log('error: ', e)
    process.exit(1)
  }
})()
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You're missing the "try" part of your try/catch.

You're also exiting the script before the file is written.

try writeFileSync instead.

const fs = require("fs");

(async() => {
  const test = [
    'test1',
    'test2'
  ]
  try {
    fs.writeFileSync('file.txt', test, {
      flag: "a"
    });
    process.exit(1);
  } catch (e) {
    console.log('error: ', e)
    process.exit(1)
  }
})()
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!