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

169
Views
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 answers
Answer question

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 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!