Working with folders in Node.js
The Node.js fs
core module provides many handy methods you can use to work with folders.
Check if a folder exists
Use fs.access()
(and its promise-based fsPromises.access()
counterpart) to check if the folder exists and Node.js can access it with its permissions.
Create a new folder
Use fs.mkdir()
or fs.mkdirSync()
or fsPromises.mkdir()
to create a new folder.
const import fs
fs = require: any
require('node:fs');
const const folderName: "/Users/joe/test"
folderName = '/Users/joe/test';
try {
if (!import fs
fs.existsSync(const folderName: "/Users/joe/test"
folderName)) {
import fs
fs.mkdirSync(const folderName: "/Users/joe/test"
folderName);
}
} catch (var err: unknown
err) {
var console: Console
console.Console.error(...data: any[]): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/error_static)error(var err: unknown
err);
}
Read the content of a directory
Use fs.readdir()
or fs.readdirSync()
or fsPromises.readdir()
to read the contents of a directory.
This piece of code reads the content of a folder, both files and subfolders, and returns their relative path:
const import fs
fs = require: any
require('node:fs');
const const folderPath: "/Users/joe"
folderPath = '/Users/joe';
import fs
fs.readdirSync(const folderPath: "/Users/joe"
folderPath);
You can get the full path:
fs.readdirSync(folderPath).map(fileName: any
fileName => {
return path.join(folderPath, fileName: any
fileName);
});
You can also filter the results to only return the files, and exclude the folders:
const import fs
fs = require: any
require('node:fs');
const const isFile: (fileName: any) => any
isFile = fileName: any
fileName => {
return import fs
fs.lstatSync(fileName: any
fileName).isFile();
};
import fs
fs.readdirSync(folderPath)
.map(fileName: any
fileName => {
return path.join(folderPath, fileName: any
fileName);
})
.filter(const isFile: (fileName: any) => any
isFile);
Rename a folder
Use fs.rename()
or fs.renameSync()
or fsPromises.rename()
to rename folder. The first parameter is the current path, the second the new path:
const import fs
fs = require: any
require('node:fs');
import fs
fs.rename('/Users/joe', '/Users/roger', err: any
err => {
if (err: any
err) {
var console: Console
console.Console.error(...data: any[]): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/error_static)error(err: any
err);
}
// done
});
fs.renameSync()
is the synchronous version:
const import fs
fs = require: any
require('node:fs');
try {
import fs
fs.renameSync('/Users/joe', '/Users/roger');
} catch (var err: unknown
err) {
var console: Console
console.Console.error(...data: any[]): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/error_static)error(var err: unknown
err);
}
fsPromises.rename()
is the promise-based version:
const import fs
fs = require: any
require('node:fs/promises');
async function function example(): Promise<void>
example() {
try {
await import fs
fs.rename('/Users/joe', '/Users/roger');
} catch (function (local var) err: unknown
err) {
var console: Console
console.Console.log(...data: any[]): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)log(function (local var) err: unknown
err);
}
}
function example(): Promise<void>
example();
Remove a folder
Use fs.rmdir()
or fs.rmdirSync()
or fsPromises.rmdir()
to remove a folder.
const import fs
fs = require: any
require('node:fs');
import fs
fs.rmdir(dir, err: any
err => {
if (err: any
err) {
throw err: any
err;
}
var console: Console
console.Console.log(...data: any[]): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)log(`${dir} is deleted!`);
});
To remove a folder that has contents use fs.rm()
with the option { recursive: true }
to recursively remove the contents.
{ recursive: true, force: true }
makes it so that exceptions will be ignored if the folder does not exist.
const import fs
fs = require: any
require('node:fs');
import fs
fs.rm(dir, { recursive: boolean
recursive: true, force: boolean
force: true }, err: any
err => {
if (err: any
err) {
throw err: any
err;
}
var console: Console
console.Console.log(...data: any[]): void
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)log(`${dir} is deleted!`);
});