sindresorhus / trash

Move files and directories to the trash
MIT License
2.57k stars 78 forks source link

Readme should mention how to include the binaries #99

Closed Venryx closed 4 years ago

Venryx commented 4 years ago

Title. I expected the library to automatically locate the binary files for the given platform, but instead it simply searches the working directory for the binaries. (requiring manual copying/distribution-alongside)

If you don't do so, you get this error (added for searches):

Error: spawn C:\...program path...\Dist\windows-trash.exe ENOENT

I ended up solving it by calling the below before using the library:

function EnsureTrashLibReady() {
    const binaryFileName =
        process.platform == "darwin" ? "macos-trash" :
        process.platform == "win32" ? "windows-trash.exe" :
        null;
    if (binaryFileName) {
        const binaryPath_src = paths.join(__dirname, "../node_modules/trash/lib", binaryFileName);
        const binaryPath_dest = paths.join(__dirname, binaryFileName);
        if (!fs.existsSync(binaryPath_dest)) {
            fs.copyFileSync(binaryPath_src, binaryPath_dest);
        }
    }
}

Even if a specific solution like the above is not included, it would be nice at least for the readme to mention that this step is necessary, and maybe offering a suggestion on how to do so.

PS. This library is trash. (ie. helpful 🤣)

sindresorhus commented 4 years ago

This is a problem with your system or how you use trash. Trash is working fine for normal Node.js usage. Trash uses the correct absolute path to the binaries: https://github.com/sindresorhus/trash/blob/d6d2c3a86125925b4e04749a8e3a311af48e93b8/lib/macos.js#L11

Venryx commented 4 years ago

Oh I see what happened. I use webpack to bundle the code for my program, and it was bundling the trash library's code into the same bundle, which was in the Dist folder.

I was able to fix it (properly) by adding this to my webpack config:

webpackConfig.externals.trash = "commonjs trash";
chuyaguo2014 commented 2 years ago

Just coming here to mention that I had the same problem (where the trash library's code was bundled with other packages and went to the wrong folder) and making a similar change in the webpack config file fixed my issue. Thanks to you both for your comments in this issue thread.