How to use the mkdir function from temp

Find comprehensive JavaScript temp.mkdir code examples handpicked from public code repositorys.

temp.mkdir is a function that creates a unique temporary directory, returning a Promise that resolves to the path of the newly created directory.

41
42
43
44
45
46
47
48
49
50
windowInitializationScript = require.resolve(
  path.join(resourcePath, 'src/initialize-application-window')
);

atomHome = await new Promise((resolve, reject) => {
  temp.mkdir('launch-', (err, rootPath) => {
    if (err) {
      reject(err);
    } else {
      resolve(rootPath);
fork icon0
star icon0
watch icon1

1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
    .catch((e) => res.status(500).json(e));
});

if (config.dev) {
  app.post(`${exports.pathPrefix}/testing/createtempdir`, ensureAuthenticated, (req, res) => {
    temp.mkdir('test-temp-dir', (err, tempPath) => res.json({ path: path.normalize(tempPath) }));
  });
  app.post(`${exports.pathPrefix}/testing/createfile`, ensureAuthenticated, (req, res) => {
    const content = req.body.content ? req.body.content : `test content\n${Math.random()}\n`;
    fs.writeFile(req.body.file, content)
fork icon0
star icon0
watch icon0

+ 5 other calls in file

How does temp.mkdir work?

temp.mkdir() is a function provided by the temp module in Node.js that creates a unique temporary directory in the system's temporary directory and returns its path as a string. The function also provides a callback function that can be used to perform operations on the created directory or to handle any errors that may occur during the creation process. This function can be useful for creating temporary directories for storing temporary files or for testing purposes.

302
303
304
305
306
307
308
309
310
311
        target: resource.name + '/' + resource.files[j].name,
      });
    }
  }
}
temp.mkdir('zipItFiles', function(error, path) {
  if (error) {
    log.error(error);
    return reject(error);
  }
fork icon0
star icon0
watch icon1

+ 5 other calls in file

336
337
338
339
340
341
342
343
344
345
  callback(null, {
    errors: [[Errors.NOT_FOUND_ERR, path]]
  })
  return
}
temp.mkdir('bracketsPackage_', function _tempDirCreated (err, extractDir) {
  if (err) {
    callback(err, null)
    return
  }
fork icon0
star icon0
watch icon1

Ai Example

1
2
3
4
5
6
7
8
const temp = require("temp");

temp.mkdir("myTempDir", function (err, dirPath) {
  if (err) {
    throw err;
  }
  console.log("Created temporary directory:", dirPath);
});

This code uses temp.mkdir to create a temporary directory with the prefix "myTempDir" and then logs the path to the console. The callback function receives an error object (if there was an error) and the path to the created directory.

27
28
29
30
31
32
33
34
35
36

/**
 * Creates the temporary directory, storing the path in memory
 */
createTempDir: () =>
  temp.mkdir("keyringbot", (err, dirPath) => {
    if (err) {
      console.error(`Error creating temp directory: ${err}`);
    }
    tempDirectory = dirPath;
fork icon0
star icon0
watch icon2