I am using react with Typescript in my project. I have initially created my app using create-react-app. Now, I want to write some data to.json file but getting this error
ReferenceError: require is not defined
Code:
let to = { tracks:[this.Array] }
const fs = require('fs');
let data = JSON.stringify(to, null, 2);
fs.writeFileSync('./file.json', data);
The file in which i have written this has .ts
extension. I am new to react and typescript and have been but didn't find anything.
You have to use import
statements instead of require
.
In react you can use the import keyword to import functions, files.
Example: import fs from './fs';
This will import the default function export from the fs to let them use the functions fs.writeFileSync() etc.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.