jprichardson / node-fs-extra

Node.js: extra methods for the fs object like copy(), remove(), mkdirs()
MIT License
9.43k stars 775 forks source link

Error when trying to copying a folder that was just made #1009

Closed NotSpaulding closed 1 year ago

NotSpaulding commented 1 year ago

I have the following code:

import { copy, ensureDir } from 'fs-extra';

const path = '/tmp/example1';
await ensureDir(path);
await copy('/tmp/example2', path);

which for some reason throws the following error:

[Error: ENOENT: no such file or directory, scandir '/tmp/example1'] {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'scandir',
  path: '/tmp/example1'
}

I have also tried the following without any luck:

import { copy, ensureDir, pathExists } from 'fs-extra';

function wait(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

const path = '/tmp/example1';
await ensureDir(path);
const check = async () => await pathExists(path) ? copy('/tmp/example2', path) : await wait(500).then(check);

await check();

but unfortunately it returns the same error.

How can I fix this error? Am I missing something?

Notes

The actual paths are different, just using those for demonstration purposes, hopefully that doesn't have an effect on this.

RyanZim commented 1 year ago

Hm, that's strange. If you create the directory manually instead of using ensureDir, do you get the same error?

NotSpaulding commented 1 year ago

No, creating the directory myself (or re-running the script because the directory gets made either way) does not produce any errors.

RyanZim commented 1 year ago

OK, just realized I was misreading your code a little; why does your script have to create this directory in the first place? You should be able to just copy to that location without ensureDir.

Also, what's the stack trace of the error? I'm curious where it's being thrown from in the fs-extra code.

NotSpaulding commented 1 year ago

After looking a bit more into this I figured out it was some other part of the code that I didn't even realize was getting called. Thanks for pointing out I don't need ensureDir to be called though.

My apologies for any confusion caused.

RyanZim commented 1 year ago

Not a problem; glad you got it figured out.