How to create a tmp dir in node without collisions

34,895

Solution 1

You can try package "tmp". It has a configuration parameter "template" which in turn uses Linux's mkstemp function which probably solves all your requirements.

Solution 2

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

Solution 3

A simple way to create unique directories would be using universally unique identifiers (UUIDs) within the path name.

Here is an example using pure-uuid:

const fs = require('fs-extra');
const path = require('path');
const UUID = require('pure-uuid');

const id = new UUID(4).format();
const directory = path.join('.', 'temp', id);

fs.mkdirs(directory).then(() => {
  console.log(`Created directory: ${directory}`);
});

You will get an output like this:

Created directory: temp\165df8b8-18cd-4151-84ca-d763e2301e14

Note: In the code above I am using fs-extra as a drop-in replacement for fs, so you don't have to care about mkdir -p because fs-extra will create the directory and any necessary subdirectories.

Tip: If you want to save your directories within the operation system's default temp directory, then you can make use of os.tmpdir(). Here's an example of how this works:

const fs = require('fs-extra');
const os = require('os');
const path = require('path');
const UUID = require('pure-uuid');

const id = new UUID(4).format();
const directory = path.join(os.tmpdir(), id);

fs.mkdirs(directory).then(() => {
  console.log(`Created directory: ${directory}`);
});

Created directory: C:\Users\bennyn\AppData\Local\Temp\057a9978-4fd7-43d9-b5ea-db169f222dba

Share:
34,895
BonsaiOak
Author by

BonsaiOak

Updated on July 19, 2022

Comments

  • BonsaiOak
    BonsaiOak almost 2 years

    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

  • BonsaiOak
    BonsaiOak about 8 years
    What if someone else created a directory while I was doing if (!stats.isDirectory())
  • Marecky
    Marecky almost 3 years
    Should I install another package in my repository which already has 16657 dependencies?... Where is it going?