I am getting the error Uncaught TypeError: fs.writeFile is not a function
in my program, in which I just want to write something to a JSON file. The fs.readFileSync
function works properly, but fs.writeFile
doesn't for some reason. Here is my code:
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h1>Hello World!</h1>
<button id="button">Write Something</button>
<script src="./app.js"></script>
</body>
</html>
JavaScript:
const fs = require("fs");
const data = fs.readFileSync("db.json", "utf8");
const db = JSON.parse(data);
console.log(db);
document.getElementById("button").onclick = () => {
fs.writeFile("db.json", "test", () => {
console.log("Written file!");
});
};
JSON:
["something1", "something2"]
Just to let you know, I am using Parcel as my bundler. Please help me on why this is not working.