fs.rename()

Renames or moves a file or directory.

Since Node 10 (fs/promises) Spec ↗

Syntax

await rename(oldPath, newPath)

Parameters

NameTypeRequiredDescription
oldPath string | Buffer | URL Yes The existing path.
newPath string | Buffer | URL Yes The destination path.

Returns

Promise<void> — Resolves once the rename completes.

Examples

import { rename } from 'node:fs/promises';

await rename('./draft.txt', './final.txt');
console.log('moved');
Output
moved
import { writeFile, rename } from 'node:fs/promises';

await writeFile('./data.json.tmp', '{"ok":true}');
await rename('./data.json.tmp', './data.json');

Notes

Renaming across filesystems or drives fails with `EXDEV`; copy then delete in that case. Within one filesystem rename is atomic, making the temp-file-then-rename pattern safe for crash-consistent writes.

See also