DEV Community

Amit Prajapati
Amit Prajapati

Posted on

Reading file in nodejs with use of module fs and path

Hi!

By asynchronously

const fs = require("fs"); const path = require("path"); const fileUrl = path.join(__dirname, "test.txt"); fs.readFile(fileUrl,(err,data)=>{ console.log("fileData: ",data) }); 
Enter fullscreen mode Exit fullscreen mode

By synchronously

const fs = require("fs"); const path = require("path"); const fileUrl = path.join(__dirname, "test.txt"); const fileData = fs.readFileSync(fileUrl); console.log(fileData ) 
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
naucode profile image
Al - Naucode

Hey, that was a nice read, you got my follow, keep writing 😉