fs मॉड्यूल
fs module आपके computer पर files और folders पढ़ता और लिखता है।
In this page:
Syntax
const fs = require('fs');
const data = fs.readFileSync('path', 'utf8');
fs.readFile('path', 'utf8', (err, data) => {
// handle err, use data
});
fs Module
fs synchronous, callback, और promise APIs offer करता है। readFileSync जैसे sync versions block करते हैं, जो scripts के लिए ठीक है, जबकि async वाले servers के लिए suit करते हैं। Buffers की बजाय strings पाने के लिए encoding की तरह utf8 इस्तेमाल करें।
Note:
Servers में event loop block करने से बचने के लिए fs.promises या callbacks prefer करें।
उदाहरण: fs module
const fs = require("fs");
fs.writeFileSync("note.txt", "hello fs");
console.log(fs.readFileSync("note.txt", "utf8"));
console.log("exists:", fs.existsSync("note.txt"));
console.log("size:", fs.statSync("note.txt").size);
// Output:
// hello fs
// exists: true
// size: 8
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- request handlers के अंदर sync methods इस्तेमाल करना
- encoding भूल जाना और एक Buffer पाना
- ENOENT जैसी errors handle न करना
चैप्टर सारांश
- fs files पढ़ता और लिखता है
- Sync, callback, और promise variants
- Text के लिए utf8 pass करें
- ENOENT जैसी errors handle करें
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: