res.sendFile throws 'path must be absolute or specify root'
TypeError: path must be absolute or specify root to res.sendFileShort 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.
Fix it fast
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_modules2Match what you see, then jump to its fix
What does the error look like?
TypeError: path must be absolute or specify root to res.sendFileres.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_modules2Fix
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
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 sendFile2Fix
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.pdfConfirmed by raw.githubusercontent.com
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
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