Skip to content
Advertisement

Copy folder recursively in Node.js

Is there an easier way to copy a folder and all its content without manually doing a sequence of fs.readir, fs.readfile, fs.writefile recursively?

I am just wondering if I’m missing a function which would ideally work like this:

fs.copy("/path/to/source/folder", "/path/to/destination/folder");

Regarding this historic question. Note that fs.cp and fs.cpSync can copy folders recursively and are available in Node v16+

Advertisement

Answer

Since Node v16.7.0 it is possible to use fs.cp or fs.cpSync function.

fs.cp(src, dest, {recursive: true});

Current stability (in Node v18.7.0) is Experimental.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement