The os.tmpdir() method is an inbuilt application programming interface of the os module which is used to get path of default directory for temporary files of the operating system.
Syntax:
os.tmpdir()
Parameters: This method does not accept any parameters.
Return Value: This method returns a string specifies the path to the default directory for temporary files of the operating system.
Below examples illustrate the use of os.tmpdir() method in Node.js:
Example 1:
// Node.js program to demonstrate the // os.tmpdir() method // Allocating os module const os = require( 'os' );
// Display os.tmpdir() value console.log(os.tmpdir()); |
Output:
C:\Users\gekcho\AppData\Local\Temp
Example 2:
// Node.js program to demonstrate the // os.tmpdir() method // Allocating os module const os = require( 'os' );
console.log( "home directory:" + getUserHome());
console.log( "temp directory:" + os.tmpdir());
function getUserHome() {
// From process.env
return process.env[(process.platform == 'win32' )
? 'USERPROFILE' : 'HOME' ];
} |
Output:
home directory:C:\Users\gekcho temp directory:C:\Users\gekcho\AppData\Local\Temp
Note: The above program will compile and run by using the node index.js
command.
Reference: https://nodejs.org/api/os.html#os_os_tmpdir