Sometimes, we want to copy a file quickly in Node.js.
In this article, we’ll look at how to copy a file quickly in Node.js.
How to copy a file quickly in Node.js?
To copy a file quickly in Node.js, we can use the fs.copyFile
method.
For instance, we write
const fs = require('fs');
fs.copyFile('source.txt', 'destination.txt', (err) => {
if (err) {
throw err;
}
console.log('source.txt was copied to destination.txt');
});
to call copyFile
with the path of the file to copy, the path to copy to, and a callback that’s run copyFile
is done.
If err
is set, that means an error occurred when copying.
Otherwise, the source file has been copied successfully.
Conclusion
To copy a file quickly in Node.js, we can use the fs.copyFile
method.