Node.js fs.mkdtempSync() Method
Last Updated :
12 Oct, 2021
Improve
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:
// Node.js program to demonstrate the // fs.mkdtempSync() method // It includes fs module const fs = require( 'fs' ); // It includes os module const os = require( 'os' ); // It includes path module const path = require( 'path' ); // Return the created folder console.log(fs.mkdtempSync( path.join(os.tmpdir(), 'foo-' ))); |
Output:
/tmp/foo-OkEvul
Example 2:
// Node.js program to demonstrate the // fs.mkdtempSync() method // It includes fs module const fs = require( 'fs' ); // It includes os module const os = require( 'os' ); const tmpDir = os.tmpdir(); const { sep } = require( 'path' ); // Print something similar to `/tmp/abc123`. // A new temporary directory is created within // the /tmp directory. console.log(fs.mkdtempSync(`${tmpDir}${sep}`)); |
Output:
/tmp/bGVto1
Reference: https://nodejs.org/api/fs.html#fs_fs_mkdtempsync_prefix_options