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

247
Views
¿Cómo puedo arrojar un error si existe y dejar de escribir con el nodo fs?

He encontrado un error al escribir una variable incorrecta, al escribir el archivo, debido a ese error, el archivo se ha escrito mal, borrando todo lo que había en él, dejándolo en blanco.

 fs.writeFile( path.join(__dirname,"../example/path/file.json") , JSON.stringify(var) );

¿Cómo puedo hacer: "Si existe un error, no intente escribir el archivo y lanzar un error a través de la consola"

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Lees la documentación y abres/escribes en el archivo con las banderas correctas :

 const fs = require('fs/promises'); async function writeFile(fn, content) { const opts = { // assuming that content is a string encoding: 'utf8', // 'wx' opens a file for writing. // The file is created if it does not exist, // and rejects if the file does exist. flag: 'wx', }; // returns undefined on success, or an error return await fs .writeFile(fn, content, opts) .catch( err => err ) ; }
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!