fsPromises.copyFile(src, dest[, mode])


    异步地将 src 复制到 dest。 默认情况下,如果 dest 已经存在,则会被覆盖。

    • src <string> | <Buffer> | <URL> source filename to copy
    • dest <string> | <Buffer> | <URL> destination filename of the copy operation
    • mode <integer> Optional modifiers that specify the behavior of the copy operation. It is possible to create a mask consisting of the bitwise OR of two or more values (e.g. fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE) Default: 0.
      • fs.constants.COPYFILE_EXCL: The copy operation will fail if dest already exists.
      • fs.constants.COPYFILE_FICLONE: The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then a fallback copy mechanism is used.
      • fs.constants.COPYFILE_FICLONE_FORCE: The copy operation will attempt to create a copy-on-write reflink. If the platform does not support copy-on-write, then the operation will fail.
    • Returns: <Promise> Fulfills with undefined upon success.

    Asynchronously copies src to dest. By default, dest is overwritten if it already exists.

    No guarantees are made about the atomicity of the copy operation. If an error occurs after the destination file has been opened for writing, an attempt will be made to remove the destination.

    import { copyFile, constants } from 'node:fs/promises';
    
    try {
      await copyFile('source.txt', 'destination.txt');
      console.log('source.txt was copied to destination.txt');
    } catch {
      console.log('The file could not be copied');
    }
    
    // By using COPYFILE_EXCL, the operation will fail if destination.txt exists.
    try {
      await copyFile('source.txt', 'destination.txt', constants.COPYFILE_EXCL);
      console.log('source.txt was copied to destination.txt');
    } catch {
      console.log('The file could not be copied');
    }