Nearby lessons

9 of 21

📁 Node.js – FS (File System) Module

📌 What is FS Module?

What is the FS Module?

The FS module is used to provide file handling in Node.js. File handling refers to the process of storing and retrieving data from files using a program. Node.js File System module allows you to work with the file system on your computer.

To include the FS module, use:

var fs = require('fs');

⚙️ Common Uses of FS Module

  • Read files
  • Create files
  • Update files
  • Delete files
  • Rename files
  • Copy files

📝 Create Files Using FS Module

Methods for creating new files:
fs.writeFile() – Asynchronous Method
fs.writeFileSync() – Synchronous Method
Node.js Environment
RUNTIME ACTIVE
// Choose which example to run by requiring either file:

// Uncomment the line below to run sync example
require('./sync-example.js');

// Uncomment the line below to run async example
require('./async-example.js');
Terminal Output
Live Output Preview

📖 Read Files Using FS Module

Node.js Environment
RUNTIME ACTIVE
// Choose which example to run:

require('./read-sync.js');
require('./read-async.js');

Terminal Output
Live Output Preview

✏️ Update Files Using FS Module

Node.js Environment
RUNTIME ACTIVE
// Choose which example to run:

require('./update-sync.js');
require('./update-async.js');

Terminal Output
Live Output Preview

🗑 Delete Files Using FS Module

Node.js Environment
RUNTIME ACTIVE
// Choose which example to run:

require('./delete-sync.js');
require('./delete-async.js');

Terminal Output
Live Output Preview

🔄 Rename Files Using FS Module

Node.js Environment
RUNTIME ACTIVE
// Choose which example to run:

require('./rename-sync.js');
require('./rename-async.js');

Terminal Output
Live Output Preview

📋 Copy Files Using FS Module

Node.js Environment
RUNTIME ACTIVE
// Choose which example to run:

require('./copy-sync.js');
require('./copy-async.js');

Terminal Output
Live Output Preview

💡 Real World Use

The FS module is essential for performing file system operations in Node.js applications. It is used to read configuration files, write log files, and manipulate files efficiently.

❌ Can we achieve file handling in core JavaScript?

Answer: No, Node.js FS module is required for file operations.

🧠 Test Your Knowledge

4 Questions

Progress: 0 / 4
Keep Going!Node.js Workflow Explained