ENOENT: no such file or directory, open
Last verified:
The fix
ENOENT: no such file or directory, open: the upload used the original filename, spaces included, as a path. Store the file under a key you generate.
- 1
Keep the real error
Upload a file whose name contains a space and read the server log. You want the stack trace that names the path or the shell command. If the handler catches the error and returns
{ ok: true }or a blank 500, remove that catch before changing anything else. The bug is the path, and hiding the exception makes the next attempt guess. - 2
Find every use of the original name
Search the upload route for
file.name,originalname,filename, and string concatenation intopath.join, a storageKey, orexec/spawnwith a shell. A space in the name is enough to break a shell string. A name like../../etc/passwdis enough to escape a folder when the raw string is joined onto a directory. - 3
Store the object under a key you generate
Write the bytes to a UUID key. Keep only a short, safe extension from the original name. Save the original name in a database column if the UI needs to show it. Do not pass the original name to a shell.
import { randomUUID } from "node:crypto"; import path from "node:path"; function storageKey(originalName: string): string { const base = path.basename(originalName); const ext = path.extname(base).toLowerCase(); const safeExt = /^\.[a-z0-9]{1,8}$/.test(ext) ? ext : ""; return `${randomUUID()}${safeExt}`; } - 4
Reject a path, and show the validation error
If the name is empty, contains a null byte, or
path.basenamechanges it (it included a slash or..), return 400 with a sentence the form can show. Encode any display name you put in a URL withencodeURIComponent. Do not catch this error and continue the upload.const label = path.basename(originalName); if (label.length === 0 || label !== originalName || label.includes("\0")) { return Response.json( { error: "That file name is not allowed. Rename the file and try again." }, { status: 400 }, ); }
Why it happens
The browser sends the file’s original name in the multipart body (my file.png). AI-generated upload code often uses that string as a filesystem path, an object-storage key, or an argument to a shell command. A space splits the command, and .. or a slash can point the path outside the upload folder. The process then throws, and a surrounding empty catch turns the crash into a generic 500.
The name is user input. It is fine as a display label. It is not a safe path segment. Storage should use a key you generate. If a URL must include a label, encode it.
The next error you'll probably hit
Once this is fixed, the usual follow-up is Body exceeded 1 MB limit. A Server Action rejected the request because the body passed Next.js's default 1 MB limit, or Vercel returned 413 FUNCTION_PAYLOAD_TOO_LARGE because the body passed 4.5 MB.
Prompt to paste back into your AI tool
Paste this above the error.
An upload fails when the file name contains spaces. The error is: <paste the real server error, including the filename and the stack> Do not hide this bug. Do not wrap the handler in an empty catch, do not return a fake success, and do not delete the log line. 1. Find where the original filename is joined into a filesystem path, an object-storage key, a URL, or a shell command. 2. Stop passing the raw name to the shell. Store the object under a UUID key. Keep a sanitized display name only as data, not as a path. 3. If the name is empty or contains a path separator, return a clear 400 the UI can show. 4. Change only the upload path handling. Do not rewrite unrelated files.
Common questions
+The file uploads from curl and fails in the browser. Is this CORS?
Only if the browser console says the response was blocked by CORS. A 500 whose log mentions the filename is this bug. The request reached your server, and the handler crashed on the name.
+Can I just replace spaces with underscores?
That fixes the space and leaves slashes, .., and shell metacharacters in the path. Generate the storage key yourself. Replacing spaces is not a sanitizer.
Related errors
- Body exceeded 1 MB limitA Server Action rejected the request because the body passed Next.js's default 1 MB limit, or Vercel returned 413 FUNCTION_PAYLOAD_TOO_LARGE because the body passed 4.5 MB.Cursorv0BoltNext.jsVercelOct 26, 2026
- it keeps rewriting the same fileit keeps rewriting the same file: the new error is in code the model added to hide the last one. Revert that diff and paste the original error once.CursorLovableBoltNext.jsReactOct 14, 2026
- has been blocked by CORS policyThis request has been blocked by CORS policy. The API sent no Access-Control-Allow-Origin for this page. Allow that origin on your server, or proxy it.CursorLovableBoltNext.jsNodeOct 14, 2026