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

158
Vistas
How to read and write the content in a JS file

I have a js file that contains some dictionary structure like below example-

File: read_js.js

export default {
  title: 'Backups',
  docTitle: 'Backups',
  navTitle: 'Backups',
  headers: {
    source_name: 'Source Volume (Path)',
    source_type: 'Type',
    source_zone: 'Source Zone',
  }
}

I want to add some data in this dictionary using typescript. How can I achieve this? I tried fs.readFileSync('read_js.js', 'utf8') but this returns all the text present in the file so couldn't read the dictionary and append my own key-value and re-write into the js file.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It returns value as String: https://nodejs.org/api/fs.html#fsreadfilesyncpath-options

so you can use it after converting to JSON Object with JSON.parse

If you want to export your object and use it, you can import it as it:

https://codesandbox.io/s/holy-frost-7ueevg?file=/src/index.js

for short:

import MyObject from "./a"; // with the name you want

console.log(MyObject); // you can get this

// if you want to "change", you needs to copy and use it for immutability.
const changed = { ...MyObject }; // ES6 spread operator to copy

I have no clue what is your environments, but it works in browser as you can see.

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