Node.js 26.14 Adds fs.mkstemp for Safe Temp Files
Node.js 26.14.0 "Lithium" landed on November 30, 2026 as an LTS release, and among its notable changes is a small addition with an outsized security story: fs.mkstemp(). It creates a unique temporary file and opens it in a single operation — closing a race that hand-rolled temp-file code has been losing for years.
The problem with hand-rolled temp files
Almost every codebase grows a helper that looks something like this:
// Don't do this
import { writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
const path = join(tmpdir(), `upload-${Date.now()}-${Math.random()}`);
await writeFile(path, buffer);Two things are wrong here, and neither is obvious in review.
The first is the gap between choosing a name and creating the file. Between those two steps, any other process on the machine can create something at that path — most usefully, a symlink pointing somewhere valuable. The write then follows the link and lands wherever the attacker chose, with the privileges of your process. This is a classic time-of-check-to-time-of-use race, and on a shared /tmp it is reachable by any local user.
The second is the name itself. Date.now() and Math.random() are not secrets. A name that can be guessed can be pre-created, and writeFile will happily open a file that someone else already owns.
The usual workaround has been fs.mkdtemp(), which has been around since Node.js 5.10. It creates a temporary directory atomically, and you then write files inside it. That works, but it means an extra directory per file, cleanup of both, and a mental model most developers skip.
What changed
fs.mkstemp() creates and opens the file itself. The official documentation states the guarantee plainly:
The file is created and opened in a single operation, so another process cannot create a file with the same name in between. On POSIX systems, the file is only readable and writable by its owner.
That is the whole point: there is no window, and the permissions are restrictive by default rather than inherited from the process umask.
Three variants shipped, all marked Stability: 1 - Experimental:
fsPromises.mkstemp(prefix[, options])— resolves with{ path, handle }, wherehandleis aFileHandlefs.mkstemp(prefix[, options], callback)— calls back with(err, file), wherefileis{ path, fd }fs.mkstempSync(prefix[, options])— returns{ path, fd }
A unique name is produced by appending six random characters to the prefix you provide.
A practical example
The promise form is the one most code will reach for:
import { mkstemp } from 'node:fs/promises';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
const { path, handle } = await mkstemp(join(tmpdir(), 'foo-'));
try {
await handle.writeFile('some data');
console.log(path);
// Prints: /tmp/foo-itXde2
} finally {
await handle.close();
}Note that you get the open handle and the path. The handle is what makes the guarantee useful — reopening by path would reintroduce the race you just avoided.
Caveats worth knowing
Nothing is cleaned up for you. The documentation is explicit: the file is not removed automatically, and closing the handle and removing the file are the caller's responsibility. A try/finally closes the handle; removing the file needs its own unlink.
The prefix is a prefix, not a directory. Random characters are appended directly to whatever string you pass. To land inside a directory, the prefix must end with the platform path separator (path.sep) or include the beginning of the filename. mkstemp(tmpdir()) writes a sibling of the temp directory, not a file in it.
Avoid trailing `X` characters. Platforms disagree here. The BSDs can return more than six random characters and will replace trailing X characters in the prefix with random ones, so a prefix like job-XXXXXX behaves differently across operating systems.
It is experimental. Stability 1 means the signature can still change. It is in an LTS line, which makes it safe to depend on operationally, but worth pinning behind a small wrapper rather than scattering across a codebase.
Takeaway
Temporary files are infrastructure that most teams never audit, and the insecure pattern is the one that reads most naturally. fs.mkstemp() makes the correct version shorter than the wrong one — which is the only reliable way a security primitive ever gets adopted. If you are planning a move to the current LTS line, it belongs on the list alongside the rest of the Node.js 26 upgrade surface.
Primary source: Node.js 26.14.0 release notes and the fs.mkstemp() documentation.