The Node.js fs core module provides many handy methods you can use to work with folders.
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.
Use fs.mkdir() or fs.mkdirSync() or fsPromises.mkdir() to create a new folder.
const fs = require('node:fs'); const folderName = '/Users/joe/test'; try { if (!fs.existsSync(folderName)) { fs.mkdirSync(folderName); } } catch (err) { console.error(err); }
import fs from 'node:fs'; const folderName = '/Users/joe/test'; try { if (!fs.existsSync(folderName)) { fs.mkdirSync(folderName); } } catch (err) { console.error(err); }
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 fs = require('node:fs'); const folderPath = '/Users/joe'; fs.readdirSync(folderPath);
import fs from 'node:fs'; const folderPath = '/Users/joe'; fs.readdirSync(folderPath);
You can get the full path:
fs.readdirSync(folderPath).map(fileName => { return path.join(folderPath, fileName); });
You can also filter the results to only return the files, and exclude the folders:
const fs = require('node:fs'); const isFile = fileName => { return fs.lstatSync(fileName).isFile(); }; fs.readdirSync(folderPath) .map(fileName => { return path.join(folderPath, fileName); }) .filter(isFile);
import fs from 'node:fs'; const isFile = fileName => { return fs.lstatSync(fileName).isFile(); }; fs.readdirSync(folderPath) .map(fileName => { return path.join(folderPath, fileName); }) .filter(isFile);
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 fs = require('node:fs'); fs.rename('/Users/joe', '/Users/roger', err => { if (err) { console.error(err); } // done });
import fs from 'node:fs'; fs.rename('/Users/joe', '/Users/roger', err => { if (err) { console.error(err); } // done });
fs.renameSync() is the synchronous version:
const fs = require('node:fs'); try { fs.renameSync('/Users/joe', '/Users/roger'); } catch (err) { console.error(err); }
import fs from 'node:fs'; try { fs.renameSync('/Users/joe', '/Users/roger'); } catch (err) { console.error(err); }
fsPromises.rename() is the promise-based version:
const fs = require('node:fs/promises'); async function example() { try { await fs.rename('/Users/joe', '/Users/roger'); } catch (err) { console.log(err); } } example();
import fs from 'node:fs/promises'; try { await fs.rename('/Users/joe', '/Users/roger'); } catch (err) { console.log(err); }
Use fs.rmdir() or fs.rmdirSync() or fsPromises.rmdir() to remove a folder.
const fs = require('node:fs'); fs.rmdir(dir, err => { if (err) { throw err; } console.log(`${dir} is deleted!`); });
import fs from 'node:fs'; fs.rmdir(dir, err => { if (err) { throw err; } console.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 fs = require('node:fs'); fs.rm(dir, { recursive: true, force: true }, err => { if (err) { throw err; } console.log(`${dir} is deleted!`); });
import fs from 'node:fs'; fs.rm(dir, { recursive: true, force: true }, err => { if (err) { throw err; } console.log(`${dir} is deleted!`); });