I have a need a create a temporary "scratch" directory on-demand in node.js. The requirements are:
/tmp/aDIge4G/
/tmp
which may already have other randomly named directories.In other words, I need the answer to this question but for directories, not files.
This answer says that what I want to do can be accomplished by mkdir -p
, but Node doesn't have the -p
flag for fs.mkdir
var tmp = require('tmp'); var tmpObj = tmp. fileSync({ mode: 0644, prefix: 'projectA-', postfix: '. txt' }); console. log("File: ", tmpObj.name); console.
Use mktemp -d . It creates a temporary directory with a random name and makes sure that file doesn't already exist. You need to remember to delete the directory after using it though.
TMPDIR is the canonical environment variable in Unix and POSIX that should be used to specify a temporary directory for scratch space. Most Unix programs will honor this setting and use its value to denote the scratch area for temporary files instead of the common default of /tmp or /var/tmp.
The current node api propose to create a temporary folder : https://nodejs.org/api/fs.html#fs_fs_mkdtemp_prefix_options_callback
which gives :
fs.mkdtemp(path.join(os.tmpdir(), 'foo-'), (err, folder) => { if (err) throw err; console.log(folder); // Prints: /tmp/foo-itXde2 }); // folder /tmp/foo-itXde2 has been created on the file system
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With