I'm trying to create a Blog posting project in React.js But, I'm new to Js itself.
What I'm trying to do is, reading the list of files from the directory.
const fs = require("fs");
const path = require("path");
const dirPath = path.join(__dirname,"../data/blogs/");
const getBlog = async () => {
await fs.readdir(dirPath,(err, files) => {
if(err) {
return console.log("Failed to list contents of directory: " + err)
}
console.log(files)
files.forEach((file,i) => {
fs.readFile(`${dirPath}/${file}` , "utf8" , (err,contents) => {
})
})
})
}
I tried to do it from a YouTube video. But I got an error something like this. TypeError
Do I have to explicitly install fs into my project?
Then I saw some people doing
const fs = require("fs").promises;
instead of
const fs = require("fs");
I tried that too.
I also read some documentation from the node.js(Documentation)
import { readdir } from "fs/promises";
And tried to import that too. Didn't work saying that fs/promises not found.
Idk what exactly is the problem.
Do I have to do something in package.json?
Do I have to install fs?If so how?
I saw stackoverflow answers but none provided clarity to my mind, so kindly help me out.
Thank you in advance.
I'm using React.js with Tailwindcss.
Via Active questions tagged javascript - Stack Overflow https://ift.tt/2FdjaAW
Comments
Post a Comment