NodeJS provides a couple of different methods to work with the file system. In this blog post, I will write mainly on how to create folders using Nodejs. You will learn how to create a folder, folder inside a folder and a temporary folder in Nodejs.
fs.mkdir() :
mkdir method is used to create one directory asynchronously. It is defined as below :
fs.mkdir(path[, options], callback)
Here :
path: the path to create the directory options : optional. It can take two values: recursive and mode. If you pass recursive as true, it will create the parent folder if not created. Its default value is false. mode is not supported on windows. callback: In the callback, you will get one Error object.
Example 1 :
const fs = require('fs');
fs.mkdir('./one', { recursive: false }, (err) => {
if (err) throw err;
});
Output :
Create one directory named ‘one’ inside the current folder. Works with recursive true as well.
Example 2 :
const fs = require('fs');
fs.mkdir('./one/two', { recursive: false }, (err) => {
if (err) throw err;
});
Output :
Throw one exception :
Error: ENOENT: no such file or directory, mkdir './one/two'
It will work with recursive true.
fs.mkdirSync() :
mkdirSync is defined as below :
fs.mkdirSync(path[, options])
It will create one directory synchronously. The parameters are same as mkdir().
const fs = require('fs');
fs.mkdirSync('./one/two', { recursive: true })
Output :
It creates one folder one in the current directory and one inner folder two inside one.
Temp directory :
Similar to a normal directory, we also have methods to create temporary directory :
fs.mkdtemp() :
It creates one temporary directory. Definition :
fs.mkdtemp(prefix[, options], callback)
Here, prefix: prefix to add the directory. It adds the prefix with six random characters to create one unique folder name and creates that folder. You can pass the final path of the temporary folder. options : Optional. Used to pass the encoding value. The default is utf8. callback: Callback function. It gives one Error object and the folder path.
Example 1 :
const fs = require('fs');
fs.mkdtemp('tmp-', (err, folder) => {
if (err) throw err;
console.log(folder);
});
It will create one temp directory in the current path. The directory name will be prefixed with tmp-.
Example 2 :
const fs = require('fs');
const path = require('path')
const os = require('os')
fs.mkdtemp(path.join(os.tmpdir(), 'tmp-'), (err, folder) => {
if (err) throw err;
console.log(folder);
});
It will create one temp folder inside the system temporary files folder.
Create one folder inside os temp folder without prefix :
const fs = require('fs');
const { sep, path } = require('path')
const os = require('os')
fs.mkdtemp(`${os.tmpdir()}${sep}`, (err, folder) => {
if (err) throw err;
console.log(folder);
});
os.tmpdir() returns the temp directory path and sep is the file separator.
fs.mkdtempSync() :
mdktempSync is used to create one temporary directory synchronously. It is defined as below :
fs.mkdtempSync(prefix[, options])
The parameters are the same as the above.