Maaz Zindani

Loading the index…

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

res.sendFile throws 'path must be absolute or specify root'

ErrorTypeError: path must be absolute or specify root to res.sendFile

Short answer

res.sendFile refuses a relative path unless you give it a root. Either build an absolute path with path.join(__dirname, 'public', 'index.html'), or pass the filename with the root option. Never rely on the current working directory, because it changes with how the process is started.

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

Fix it fast

  1. 1Find out which cause you have

    Find every sendFile call and look at the first argument. A string that does not start with / (or a drive letter) is the cause.

    grep -rn "sendFile" --include=*.js --include=*.ts . --exclude-dir=node_modules
  2. 2Match what you see, then jump to its fix

    1. 1TypeError: path must be absolute or specify root to res.sendFileA relative path is passed without a rootFix
    2. 2You wrote sendFile(req.params.name) or similar and hit the same TypeErrorThe file name comes from the request and no root confines itFix

What does the error look like?

  • TypeError: path must be absolute or specify root to res.sendFile
  • res.sendFile('index.html') fails but works when written with an absolute path

Cause 1 of 2

A relative path is passed without a root

1Check

Express throws this when no root option is set and the path is not absolute. './public/index.html' and 'index.html' both trigger it.

grep -rn "sendFile(['\"]" --include=*.js . --exclude-dir=node_modules

2Fix

Build an absolute path from the folder of the current file. In CommonJS use __dirname.

const path = require('path');
app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname, 'public', 'index.html'));
});

3Verify

Start the server from a different folder and request the page. It should still load.

cd / && node /path/to/app/server.js &
curl -I http://localhost:3000/

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 2

The file name comes from the request and no root confines it

1Check

A bare name from the URL is a relative path, so Express throws. The root option is the documented way to say which directory relative names resolve against.

grep -rn "req.params\|req.query" --include=*.js . --exclude-dir=node_modules | grep sendFile

2Fix

Pass the name as the first argument and the directory as the root option.

app.get('/files/:name', (req, res) => {
  res.sendFile(req.params.name, {
    root: path.join(__dirname, 'files'),
    dotfiles: 'deny',
  });
});

3Verify

A valid file name downloads and the TypeError is gone.

curl -I http://localhost:3000/files/report.pdf

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Build file paths from __dirname, never from a leading dot.
  • Use the root option whenever the file name comes from the request.

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.