Node.js fs.mkdtempSync() Method
The fs.mkdtempSync() method is an inbuilt application programming interface of fs module which provides an API for interacting with the file system in a manner closely modeled around standard POSIX functions. The fs.mkdtempSync() method creates a unique temporary directory. This is the synchronous version of fs.mkdtemp() method.
Syntax:
fs.mkdtempSync( prefix, options )
Parameters: This method accept two parameters as mentioned above and described below:
- Prefix: The six random characters are appended behind the prefix to create a unique temporary directory.
- Options: It is an optional parameter that can be a string specifying an encoding, or an object with an encoding property specifying the character encoding to use.
Return value: It returns the created folder path.
Below examples illustrate the use of fs.mkdtempSync() method in Node.js:
Example 1:
const fs = require( 'fs' );
const os = require( 'os' );
const path = require( 'path' );
console.log(fs.mkdtempSync(
path.join(os.tmpdir(), 'foo-' )));
|
Output:
/tmp/foo-OkEvul
Example 2:
const fs = require( 'fs' );
const os = require( 'os' );
const tmpDir = os.tmpdir();
const { sep } = require( 'path' );
console.log(fs.mkdtempSync(`${tmpDir}${sep}`));
|
Output:
/tmp/bGVto1
Reference: https://nodejs.org/api/fs.html#fs_fs_mkdtempsync_prefix_options
Last Updated :
12 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...