Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

285
Vistas
Delete specific rows from a text file in NodeJS

I am trying to delete a specific row from a text file using NodeJs, but I am having trouble to do so. Can anyone please help me?

This is the content inside the text file and I am trying to delete example3:example3 from the text file.

example1:example1
example2:example2
example3:example3
example4:example4

This is the code that should work but doesn't

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

const filePath = path.join(__dirname, '../data/test.txt')
const fileData = fs.readFileSync(filePath, 'utf-8').split('\r\n')

const position = 3

fileData.splice(position - 1, 1)
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need to save the file with fs.writeFile or fs.writeFileSync

Synchronous:

const fs = require('fs')
const path = require('path')
const filePath = '../data/test.txt'

const filePath = path.join(__dirname, filePath)
const fileData = fs.readFileSync(filePath, 'utf-8').split('\r\n')

const position = 3

fileData.splice(position - 1, 1)
const dataString = fileData.join('\r\n')
fs.writeFileSync(filePath, dataString)

Asynchronous:

const fs = require('fs')
const path = require('path')
const filePath = '../data/test.txt'

const filePath = path.join(__dirname, filePath )

fs.readFile(filePath, (err, data) => {
  if (err) throw err

  const fileData a data.split('\r\n')

  const position = 3

  fileData.splice(position - 1, 1)
  const dataString = fileData.join('\r\n')

  fs.writeFile(filePath, dataString, (err) => {
    if (err) throw err
    else console.log('file saved')
  })
})


about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda