fs-copy-file-sync
![Coverage Status](https://camo.githubusercontent.com/6b5f3fe00ca91c4f07f7afa07fc60507f75c5550ee73ec06170d6b63fae7363e/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f636f64657261697365722f66732d636f70792d66696c652d73796e632f62616467652e7376673f6272616e63683d6d617374657226736572766963653d676974687562)
Node.js v8.5.0 fs.copyFileSync ponyfill.
Synchronously copies src to dest. By default, dest is overwritten if it already exists. Returns undefined. Node.js makes no guarantees about the atomicity of the copy operation. If an error occurs after the destination file has been opened for writing, Node.js will attempt to remove the destination.
Install
npm i fs-copy-file-sync
API
flags
is an optional integer that specifies 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
).
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.
Example:
const copyFileSync = require('fs-copy-file-sync');
// destination.txt will be created or overwritten by default.
copyFileSync('source.txt', 'destination.txt');
console.log('source.txt was copied to destination.txt');
If the third argument is a number, then it specifies flags, as shown in the following example.
const copyFileSync = require('fs-copy-file-sync');
const { COPYFILE_EXCL } = copyFileSync.constants;
// By using COPYFILE_EXCL, the operation will fail if destination.txt exists.
fs.copyFileSync('source.txt', 'destination.txt', COPYFILE_EXCL);
Related
- fs-copy-file - Asynchronously copies src to dest.
- fs-readdir-with-file-types - Asynchronously read directory with file types.
- fs-readdir-sync-with-file-types - Synchronously read directory with file types.
License
MIT