Perform following steps,
- import path from 'path';
- Need for path of files in order to access them
- import fs from 'fs';
- Need for read and write on files
- In order to read or write, first we need to resolve the directory of the file
- path.resolve(__dirname, 'directoryPath')
- For reading, need to parse before read (reading JSON file)
- JSON.parse(fs.readFileSync(dirVar,'utf-8'))
- For writing, need to join the file name with directory (writing JSON file)
- fs.writeFileSync( join(dirVar, 'filename'), JSON.stringify(contentVar) )
No comments:
Post a Comment