0

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. I'm using React.js with Tailwindcss.

James Z
  • 12,209
  • 10
  • 24
  • 44

1 Answers1

0

Are you trying to execute the code serverside or clientside? I think fs can only be used on serverside, since it's part of nodeJS.

fre-ben
  • 90
  • 1
  • 9