fs.rename(oldPath, newPath, callback)


异步将位于 oldPath 的文件重命名为提供的 newPath 路径。如果 newPath 已存在,它将被覆盖。如果 newPath 是一个目录,则会引发错误。完成回调中除了可能的异常,不会提供其他参数。

🌐 Asynchronously rename file at oldPath to the pathname provided as newPath. In the case that newPath already exists, it will be overwritten. If there is a directory at newPath, an error will be raised instead. No arguments other than a possible exception are given to the completion callback.

另请参见:rename(2)。

🌐 See also: rename(2).

import { rename } from 'node:fs';

rename('oldFile.txt', 'newFile.txt', (err) => {
  if (err) throw err;
  console.log('Rename complete!');
});