fs Module
Use fs for basic file-system operations such as reading/writing files, creating directories, and inspecting file metadata.
javascript
import * as fs from "fs";Note
fs paths support absolute paths and relative paths. Relative paths are resolved from the entry script directory.
Script Context
fs is intended for Main script usage.
Table of Contents
fs.readFile(path)
Reads a file as text.
Parameters
path- Type:
string - Description: File path to read.
- Type:
Return Value
- Type:
string | null - Description: File content, or
nullif the file could not be opened.
fs.writeFile(path, data, [append])
Writes text to a file.
Parameters
path- Type:
string - Description: Target file path.
- Type:
data- Type:
string - Description: Text to write.
- Type:
append- Type:
boolean - Required: No
- Default:
false - Description:
trueappends to the file;falseoverwrites it.
- Type:
Return Value
- Type:
boolean - Description:
trueif write succeeded; otherwisefalse.
fs.exists(path)
Checks whether a file or directory exists.
Parameters
path- Type:
string - Description: Target path.
- Type:
Return Value
- Type:
boolean - Description:
trueif the path exists; otherwisefalse.
fs.mkdir(path, [recursive])
Creates a directory.
Parameters
path- Type:
string - Description: Directory path to create.
- Type:
recursive- Type:
boolean - Required: No
- Default:
true - Description:
truecreates intermediate directories if needed.
- Type:
Return Value
- Type:
boolean - Description:
trueif the directory exists after the call; otherwisefalse.
fs.readdir(path)
Lists names in a directory.
Parameters
path- Type:
string - Description: Directory path.
- Type:
Return Value
- Type:
string[] - Description: Array of entry names in that directory.
fs.unlink(path)
Removes a file or an empty directory.
Parameters
path- Type:
string - Description: Path to remove.
- Type:
Return Value
- Type:
boolean - Description:
trueif removed; otherwisefalse.
fs.rename(from, to)
Renames or moves a file/directory.
Parameters
from- Type:
string - Description: Existing path.
- Type:
to- Type:
string - Description: New path.
- Type:
Return Value
- Type:
boolean - Description:
trueif rename succeeded; otherwisefalse.
fs.copyFile(from, to, [overwrite])
Copies a file.
Parameters
from- Type:
string - Description: Source file path.
- Type:
to- Type:
string - Description: Destination file path.
- Type:
overwrite- Type:
boolean - Required: No
- Default:
true - Description: Whether to overwrite destination if it already exists.
- Type:
Return Value
- Type:
boolean - Description:
trueif copy succeeded; otherwisefalse.
fs.stat(path)
Returns metadata for a file or directory.
Parameters
path- Type:
string - Description: Target path.
- Type:
Return Value
- Type:
object | null - Description: Metadata object, or
nullif the path does not exist.
Returned object fields:
isFile(boolean):truewhen path is a regular file.isDirectory(boolean):truewhen path is a directory.isSymlink(boolean):truewhen path is a symbolic link.size(number): File size in bytes (0for directories).mode(number): Native permission/mode value.
Example
javascript
import * as fs from "fs";
const baseDir = __dirname + "\\tmp_fs_api";
const nestedDir = baseDir + "\\nested\\child";
const fileA = baseDir + "\\a.txt";
const fileB = baseDir + "\\b.txt";
const fileC = baseDir + "\\c.txt";
console.log("mkdir recursive:", fs.mkdir(nestedDir, true));
console.log("writeFile a:", fs.writeFile(fileA, "Hello"));
console.log("append via writeFile:", fs.writeFile(fileA, " World", true));
console.log("readFile a:", fs.readFile(fileA));
console.log("copyFile a->b:", fs.copyFile(fileA, fileB));
console.log("rename b->c:", fs.rename(fileB, fileC));
console.log("readdir baseDir:", JSON.stringify(fs.readdir(baseDir)));
console.log("stat a:", JSON.stringify(fs.stat(fileA)));
console.log("exists c:", fs.exists(fileC));
console.log("unlink a:", fs.unlink(fileA));
console.log("unlink c:", fs.unlink(fileC));