node-fs-extra
node-fs-extra copied to clipboard
Copying a directory to a path that already has a directory with that name does not throw an error even when options are set correctly.
- Operating System: Kubuntu 21.04.
- Node.js version: 16.11.0
-
fs-extra
version: 10.1.0
Copying a directory to a path that already has a directory with that name does not throw an error even when options are set correctly.
Here is the relevant output from my terminal:
$ ls -p
destDir/ index.js node_modules/ package.json package-lock.json sourceDir/
$ ls -p sourceDir/
testDir/
$ ls -p destDir/
testDir/
$ cat index.js
const fs = require('fs-extra');
const source = "./sourceDir/testDir";
const destination = "./destDir/testDir";
const options = {
overwrite: false,
errorOnExist: true,
};
fs.copy(source, destination, options, err => {
if (err) return console.error(err);
console.log('success!');
});
$ node index.js
success!
$ node -v
v16.11.0
$ cat package.json
{
"name": "fs-extra-demonstration",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"fs-extra": "^10.1.0"
}
}
As you see, fs-extra reports success even though it should have thrown an error, since "testDir" already existed at "destDir".
Please let me know if I'm missing something or if I'm doing something incorrectly!
Thank you.
Yep, looks like a bug. @manidlou?
This is not a bug, it is an expected behavior. For directories, fs-extra
copies everything inside the directory (not the directory itself). It is also mentioned in our docs https://github.com/jprichardson/node-fs-extra/blob/master/docs/copy.md.
To clarify, you're copying the contents of sourceDir/testDir
to destDir/testDir
, so if a file or directory already exists inside destDir/testDir
with the same name as the one inside the sourceDir/testDir
, then you would get an error.
:man_facepalming: I feel silly; I did know that, but I somehow forgot about it when I replied.