Learn how to interact with folders using Node.js and the fs
core module. Discover methods for checking if a folder exists, creating a new folder, reading the content of a directory, renaming a folder, and removing a folder.
Check if a folder exists
To check if a folder exists and if Node.js has access to it, use the fs.access()
method.
const fs = require('fs');
const folderPath = '/path/to/folder';
fs.access(folderPath, (err) => {
if (err) {
console.error('Folder does not exist or cannot be accessed.');
return;
}
console.log('Folder exists and can be accessed.');
});
Create a new folder
To create a new folder, use either the fs.mkdir()
or fs.mkdirSync()
method.
const fs = require('fs');
const folderPath = '/path/to/new/folder';
try {
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath);
console.log('Folder created successfully.');
} else {
console.error('Folder already exists.');
}
} catch (err) {
console.error(err);
}
Read the content of a directory
To read the contents of a directory, including files and subfolders, use either the fs.readdir()
or fs.readdirSync()
method.
This code reads the content of a folder and returns the relative path of each file and subfolder.
const fs = require('fs');
const path = require('path');
const folderPath = '/path/to/folder';
const content = fs.readdirSync(folderPath);
console.log(content);
To get the full path of each file and subfolder, use path.join()
.
const content = fs.readdirSync(folderPath).map(fileName => {
return path.join(folderPath, fileName);
});
console.log(content);
To filter the results and only return files (excluding folders), you can use the isFile()
method provided by the fs
module.
const isFile = fileName => {
return fs.lstatSync(fileName).isFile();
};
const content = fs.readdirSync(folderPath)
.map(fileName => path.join(folderPath, fileName))
.filter(isFile);
console.log(content);
Rename a folder
To rename a folder, use either the fs.rename()
or fs.renameSync()
method. Pass the current path as the first parameter and the new path as the second parameter.
const fs = require('fs');
const currentPath = '/path/to/current/folder';
const newPath = '/path/to/new/folder';
fs.rename(currentPath, newPath, err => {
if (err) {
console.error(err);
return;
}
console.log('Folder renamed successfully.');
});
The fs.renameSync()
method can be used for synchronous renaming.
try {
fs.renameSync(currentPath, newPath);
console.log('Folder renamed successfully.');
} catch (err) {
console.error(err);
}
Remove a folder
To remove a folder, use either the fs.rmdir()
or fs.rmdirSync()
method.
const fs = require('fs');
const folderPath = '/path/to/folder';
fs.rmdir(folderPath, err => {
if (err) {
console.error(err);
return;
}
console.log('Folder removed successfully.');
});
Please note that removing a folder with content can be more complicated. If you need advanced features, it is recommended to use the popular and well-maintained fs-extra
module. The remove()
method from fs-extra
provides additional functionality on top of the fs
module.
To use fs-extra
, install it via NPM:
npm install fs-extra
Then, you can remove a folder using fs.remove()
.
const fs = require('fs-extra');
const folderPath = '/path/to/folder';
fs.remove(folderPath, err => {
if (err) {
console.error(err);
return;
}
console.log('Folder removed successfully.');
});
Alternatively, you can use promises or async/await with fs-extra
.
fs.remove(folderPath)
.then(() => {
console.log('Folder removed successfully.');
})
.catch(err => {
console.error(err);
});
async function removeFolder(folderPath) {
try {
await fs.remove(folderPath);
console.log('Folder removed successfully.');
} catch (err) {
console.error(err);
}
}
removeFolder(folderPath);