File system
fs/promises.rename()
Moves or renames a directory entry on the filesystem. Tested on Node.js 22.16.0. An existing destination may be replaced; crossing filesystems can fail with EXDEV. Rename alone is not a full crash-durability guarantee.
Formal Syntax & Structure
nodejs syntaxnodejs
fsPromises.rename(oldPath, newPath)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| oldPath | string | Buffer | URL | Existing path. |
| newPath | string | Buffer | URL | Destination path. |
Return Value
Promise<void>
Resolves on success.
Working Example
Example Snippetnodejs
import * as fs from 'node:fs/promises';import { join } from 'node:path';import { tmpdir } from 'node:os';const dir=await fs.mkdtemp(join(tmpdir(),'bytemind-ref-'));try { const from=join(dir,'draft.txt'),to=join(dir,'final.txt'); await fs.writeFile(from,'ready');await fs.rename(from,to); console.log(await fs.readFile(to,'utf8'));} finally { await fs.rm(dir,{recursive:true,force:true}); }