trash()
throws the following error when using Next.js on Windows: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of URL. Received an instance of URL at async deleteAction (./app/actions/delete.ts:14:5)
Reproducable example at https://github.com/kjetilhartveit/nextjs-sindresorhus-trash.
I've determined the issue is linked to what import.meta.url
is returning in Next.js. In Next.js it looks like this "/_next/static/media/windows-trash.2b7fe177.exe". In the trash library the Windows trash binary is imported like this const binary = new URL('windows-trash.exe', import.meta.url);
. Even though an URL is created, it fails later in a call to fileURLToPath() because it is missing a protocol (e.g. file:). fileURLToPath()
validates the path using a isURL(path) there you can see it expects a protocol to be set.
My temporary fix in my side-project has been to download the recycle-bin binary directly and then copied the logic from https://github.com/sindresorhus/trash/blob/main/lib/windows.js and https://github.com/sindresorhus/trash/blob/main/lib/chunked-exec.js except that the binary was "imported" like this const pathToRecycleBinary = dirname(dirname(import.meta.url)) + "/binary/recycle-bin.exe";
Ala this:
"use server";
import { promisify } from "node:util";
import { execFile } from "node:child_process";
import { fileURLToPath } from "node:url";
import chunkify from "@sindresorhus/chunkify";
import { dirname } from "node:path";
const pathToRecycleBinary =
dirname(dirname(import.meta.url)) + "/binary/recycle-bin.exe";
const pExecFile = promisify(execFile);
export async function deleteFile(path: string) {
for (const chunk of chunkify([path], 200)) {
const urlToBinary = fileURLToPath(pathToRecycleBinary);
await pExecFile(urlToBinary, chunk);
}
}
I don't know if this is the best solution (importing the binary differently) to the problem though.
Note: not sure if this only affects Windows users
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too