Issues with copying a directory

I’m building a UXP plugin in InDesign, and I’m trying to copy a directory and its contents from one place to another. Is the documentation wrong, or am I using fs.copyFile incorrectly?

The documentation says:

Copies a file or a folder from the source path to the destination path

However, Node’s documentation for fs.copyFile only mentions using the method for files, and some other sources state it can’t be used for copying folders.

Example code:

const fs = require('fs');

// I would like to copy "Source" and all of its contents into Destination
const srcPath = "/Users/user/Desktop/Source"; // assume this exists
const destPath = "/Users/user/Desktop/Destination"; // assume this exists

const newDestPath = `${destPath}/Source`; // assume this did not exist

fs.copyFile(srcPath, newDestPath);

Manifest snippet:

"requiredPermissions": {
    "localFileSystem": "fullAccess",

This is the error that fs.copyFile keeps throwing:

Error: operation not supported on socket
    at u (uxp://uxp-internal/webfs_scripts.js:2)
    at uxp://uxp-internal/webfs_scripts.js:2

I couldn’t find any helpful advice online, so I’m wondering if the UXP documentation is incorrect.

Do I need to recursively copy each and every file into the destination directory? I would use fs.cp, but it doesn’t appear to exist in UXP yet.

To clarify, is your example code the code you’re using to test/debug?

The example code isn’t exactly the code I’m using. I removed all of the path validation and error handling for clarity. If you replaced the paths with ones on your own machine, you should get the same error in the debug console.

Gotcha. Just wanted to make sure since the paths looked like placeholders :slight_smile:. I’ve used copyFile a bunch but never with folders. That said, I’m a bit suspicious about you making the destination directory beforehand. Have you tried leaving that up to copyFile? (I have a hunch that whatever system routine is used under the hood probably complains about the destination already existing).