Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Express sendFile or static fails with ENOENT for a file that exists in your project

ErrorError: 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.

  • Applies to Express 5, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.html
  2. 2Match what you see, then jump to its fix

    1. 1stat '/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. 2The 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

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

Did this fix it?

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 public

2Fix

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.html

Confirmed by expressjs.com

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.