How to use the mkdtempSync function from fs

Find comprehensive JavaScript fs.mkdtempSync code examples handpicked from public code repositorys.

fs.mkdtempSync is a function in the Node.js fs module that creates a new temporary directory.

64
65
66
67
68
69
70
71
72
73
exports.lstat = co.promisify(fs.lstat);
exports.lstatSync = fs.lstatSync;
exports.mkdir = co.promisify(fs.mkdir);
exports.mkdirSync = fs.mkdirSync;
exports.mkdtemp = co.promisify(fs.mkdtemp);
exports.mkdtempSync = fs.mkdtempSync;
exports.open = co.promisify(fs.open);
exports.openSync = fs.openSync;
exports.read = co.promisify(fs.read);
exports.readSync = fs.readSync;
fork icon22
star icon45
watch icon26

66
67
68
69
70
71
72
73
74
75
tests['fs.sync.lstat'] = 'fs.writeFileSync("fs15.txt", "123", "utf8");' +
                         'fs.lstatSync("fs15.txt");' +
                         'fs.unlinkSync("fs15.txt")';
tests['fs.sync.mkdir'] = 'fs.mkdirSync("fstemp0");' +
                         'fs.rmdirSync("fstemp0")';
tests['fs.sync.mkdtemp'] = 'const fp = fs.mkdtempSync("fstemp1");' +
                           'fs.rmdirSync(fp)';
tests['fs.sync.open'] = 'fs.writeFileSync("fs16.txt", "123", "utf8");' +
                        'fs.unlinkSync("fs16.txt")';
tests['fs.sync.read'] = 'fs.writeFileSync("fs17.txt", "123", "utf8");' +
fork icon42
star icon19
watch icon0

How does fs.mkdtempSync work?

fs.mkdtempSync is a synchronous function in the Node.js fs module that creates a new temporary directory.

When fs.mkdtempSync is called with a prefix string, it performs the following steps:

  1. It generates a unique name for the new temporary directory by appending a random string of characters to the end of the prefix.
  2. It creates a new directory with the generated name in the system's temporary directory.
  3. It returns the path to the new directory.

By default, the system's temporary directory is determined by the TMPDIR environment variable on Unix systems, and by the TEMP or TMP environment variable on Windows systems. However, the location of the temporary directory can be changed by setting the TMPDIR environment variable on Unix systems, or by using the setTempDir method of the os module on Windows systems.

Overall, fs.mkdtempSync is a useful tool for creating temporary directories for use in various applications, such as testing or data processing. However, it's important to note that temporary directories created with fs.mkdtempSync are not automatically cleaned up, and should be manually deleted after use to avoid cluttering the file system.

651
652
653
654
655
656
657
658
659
660
661
662
  };


  fs.mkdtemp(nonexistentDir, common.mustCall(validateError));


  assert.throws(
    () => fs.mkdtempSync(nonexistentDir),
    validateError
  );
}

fork icon42
star icon19
watch icon0

523
524
525
526
527
528
529
530
531
532
533
534
    mkdirSync(context.environmentPath, {recursive: true})
    context.eventPath = `${context.testRepository}/event.yml`;
}


function tempDirectory() {
    return mkdtempSync(join(tmpdir(), randomString()));
}


async function test({given, then}) {
    const run = async (func, params) => typeof func === 'function' ? func?.(params) : await func?.(params)
fork icon1
star icon8
watch icon2

Ai Example

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

const prefix = "my-temp-dir-";

const tempDir = fs.mkdtempSync(prefix);

console.log(tempDir);

In this example, we're using the fs module to create a temporary directory. We're first creating a string called prefix with the value "my-temp-dir-". This will be used as a prefix for the name of the new temporary directory. We're then using fs.mkdtempSync to create a new temporary directory with the specified prefix. The function returns the path to the new directory. Finally, we're using console.log to output the path to the new temporary directory to the console. When we run this code, we'll get output that looks something like this: bash Copy code

253
254
255
256
257
258
259
260
261
262
    return this._info['state'];
}

getTmpDir() {
    if (!this._tmpDir)
        this._tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cms-'));
    return this._tmpDir;
}

getPathForFile(attr) {
fork icon0
star icon0
watch icon1

66
67
68
69
70
71
72
73
74
75
tests['fs.sync.lstat'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
                         'fs.lstatSync("fs.txt");' +
                         'fs.unlinkSync("fs.txt")';
tests['fs.sync.mkdir'] = 'fs.mkdirSync("fstemp");' +
                         'fs.rmdirSync("fstemp")';
tests['fs.sync.mkdtemp'] = 'const fp = fs.mkdtempSync("fstest");' +
                           'fs.rmdirSync(fp)';
tests['fs.sync.open'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
                        'fs.unlinkSync("fs.txt")';
tests['fs.sync.read'] = 'fs.writeFileSync("fs.txt", "123", "utf8");' +
fork icon0
star icon0
watch icon0

+ 11 other calls in file

58
59
60
61
62
63
64
65
66
67
68
69
  fs.realpath(__filename, options, common.mustCall(errHandler));
}


{
  const tempFileName = path.resolve(tmpdir.path, 'mkdtemp-');
  fs.mkdtempSync(tempFileName, options);
  fs.mkdtemp(tempFileName, options, common.mustCall(errHandler));
}


{
fork icon0
star icon0
watch icon0

63
64
65
66
67
68
69
70
71
72
73
74
assert.throws(() => {
  fs.mkdtemp('path', options, common.mustNotCall());
}, expectedError);


assert.throws(() => {
  fs.mkdtempSync('path', options);
}, expectedError);


assert.throws(() => {
  fs.ReadStream('path', options);
fork icon0
star icon0
watch icon0