Express sendFile or static fails with ENOENT for a file that exists in your project
Error: ENOENT: no such file or directory, stat '/public/main.html'Short answer
ENOENT from Express means the path it built does not exist on disk. A path starting with a slash is read from the filesystem root, not from your project. Pass sendFile an absolute path or a root option, and build express.static paths from __dirname so they do not depend on where node was started.
Fix it fast
1Find out which cause you have
Print the exact path Express is going to open and compare it with the real file location. The path in the error is the one that is wrong.
node -e "console.log(process.cwd())" ls -l public/main.html2Match what you see, then jump to its fix
- 1
stat '/public/main.html': the path begins at the filesystem root, not at your project foldersendFile was given a path that starts with a slashFix - 2
The site works with `node index.js` in the project root but fails from a process manager, a script or another folderexpress.static uses a relative folder and node was started from another directoryFix
- 1
What does the error look like?
Error: ENOENT: no such file or directory, stat '/public/main.html'
Cause 1 of 2
sendFile was given a path that starts with a slash
1Check
res.sendFile treats a string such as '/public/main.html' as an absolute path on the machine. Look for a sendFile call with a hand-typed path.
grep -rn "sendFile" --include=*.js .2Fix
Build an absolute path from the folder of the current file, or keep the relative name and set the root option. With root set, relative paths are allowed and Express checks they stay inside that folder.
const path = require('path');
app.get('/', (req, res, next) => {
res.sendFile('main.html', { root: path.join(__dirname, 'public') }, (err) => {
if (err) next(err);
});
});3Verify
Open the route in a browser or request it with curl.
curl -I http://localhost:3000/Confirmed by expressjs.com
Cause 2 of 2
express.static uses a relative folder and node was started from another directory
1Check
The folder given to express.static is resolved against the directory node was launched from, not against the script file. Compare the working directory with where the public folder lives.
pwd
ls -d public2Fix
Give express.static an absolute path built from __dirname. In an ES module __dirname does not exist, so derive it first.
// CommonJS
app.use(express.static(path.join(__dirname, 'public')));
// ES module
import path from 'path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
app.use(express.static(path.join(__dirname, 'public')));3Verify
Start the app from a different folder and request a file.
cd /
node /path/to/your-app/index.js
curl -I http://localhost:3000/main.htmlConfirmed by expressjs.com
How do I stop it coming back?
- Never write sendFile paths as '/folder/file'; use path.join(__dirname, ...) or the root option.
- Use absolute paths for express.static so PM2, systemd and npm scripts behave the same as your terminal.
- Pass a callback to sendFile and forward errors to next() so a missing file is logged once, with its path.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
bcrypt.hash throws 'data and salt arguments required'
Error: data and hash arguments required
The bcrypt package throws this when hash gets null or undefined for the password or for the salt.
4 causes, each with a fixBack end
bcryptjs rejects hash() or compare() with Illegal arguments
Error: Illegal arguments: string, undefined
bcryptjs checks the types of its two arguments and, when either is wrong, throws Illegal arguments followed by the typeof of the first and the second.
3 causes, each with a fixBack end
Express warns 'body-parser deprecated undefined extended'
body-parser deprecated undefined extended
The warning appears because urlencoded() was called without the extended option, which body-parser 1.x has warned about since version 1.4.0.
3 causes, each with a fixBack end
Browser blocks a stylesheet or script: MIME type mismatch with nosniff
The Network tab shows the asset URL answered with Content-Type text/html
With X-Content-Type-Options: nosniff the browser accepts a stylesheet only as text/css and a script only as a JavaScript type.
3 causes, each with a fix