I have a need a create a temporary "scratch" directory on-demand in node.js. The requirements are:
- the dirname should be randomized (i.e.
/tmp/aDIge4G/
- the directory will be created within
/tmp
which may already have other randomly named directories. - if the directory already exists, I should throw rather than use it and overwrite someone else's work
- this needs to be safe in a concurrent environment. I can't just check if the directory exists and then create it if it doesn't because someone else may have created a directory with the same name after I checked.
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