- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Async Copy in fs-extra - NodeJS
Introduction to Async copy
This method copies files or directories from one location to another location. The directory can have sub-directories and files.
Syntax
copy(src, dest[, options][, callback])
Parameters
src – This is a string paramter which will hold the source location of the file or directory that needs to be copies. If the location is a directory, it will copy everything inside of the directory instead of whole directory.
dest – This will hold the destination location where the files/directories will be copies. If src is a files, dest cannot be a directory.
options −
overwrite – If set to true, existing files or directories will be overwritten. Default value is set to true.
errorOnExist – It will throw an error if the destination file/folder exists only when overwrite is set to false.
preserveTimestamps – Latest modification and access time will be set to that of the original files if true, else it will be OS dependent.
filter – This option will filter copied files. Will include filtered files if set to true.
callback – This function will give a callback if any error occurs.
Example
Check that fs-extra is installed before proceeding, if not, install fs-exra.
You can use the following command to check whether fs-extra is installed or not.
npm ls fs-extra
Create a copyExample.js and copy-paste the following code snippet into that file.
Now, run the following command to run the code snippet.
node copyExample.js
Code Snippet
const fs = require('fs-extra') // Copies file with a callback: fs.copy('/tmp/myfile', '/tmp/mynewfile', err => { if (err) return console.error(err) console.log('success with callback!') }) // Return a callback if any error occurs // Copies directory from src to dest fs.copy('/tmp/mydir', '/tmp/mynewdir', err => { if (err) return console.error(err) console.log('Directories copied successfully!') }) // Copies file with Promises: fs.copy('/tmp/myfile', '/tmp/mynewfile') .then(() => { console.log('success with promise!') }) .catch(err => { console.error(err) }) // Copies file with async/await: async function copyExample () { try { await fs.copy('/tmp/myfile', '/tmp/mynewfile') console.log('success with await!') } catch (err) { console.error(err) } } copyExample()
Output
C:\Users\tutorialsPoint\> node copyExample.js success with callback! success with promise! success with await! Directories copied successfully!
- Related Articles
- Sync vs Async vs Async/Await in fs-extra - NodeJS
- Sync Copy in fs-extra using NodeJS
- EmptyDir() function in fs-extra - NodeJS
- ensureFile() function in fs-extra - NodeJS
- ensureSymlink() function in fs-extra - NodeJS
- outputFile() function in fs-extra - NodeJS
- pathExists() function in fs-extra - NodeJS
- readJson() function in fs-extra - NodeJS
- writeJson() function in fs-extra - NodeJS
- Async & await keyword in C#
- Async/Await Functions in JavaScript
- Introduction to ensureFileSync() in NodeJS
- Introduction to Sequelize in NodeJS
- What are async methods in JavaScript?
- Understanding execute async script in Selenium
