Open In App

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:

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


Article Tags :