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

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
4 min

banner

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

The Node.js and Express "TypeError: path must be absolute or specify root to res.sendFile" occurs when we pass a relative path to the res.sendFile method.

To solve the error, either pass an absolute path to the method or set the root property in the options object

path must be absolute or specify root to res sendfile

Here is the complete stack trace:

shell
TypeError: path must be absolute or specify root to res.sendFile at ServerResponse.sendFile (/home/borislav/Desktop/bobbyhadz-rest/bobbyhadz-js/node_modules/express/lib/response.js:441:11) at file:///home/borislav/Desktop/bobbyhadz-rest/bobbyhadz-js/index.js:7:7 at Layer.handle [as handle_request] (/home/borislav/Desktop/bobbyhadz-rest/bobbyhadz-js/node_modules/express/lib/router/layer.js:95:5) at next (/home/borislav/Desktop/bobbyhadz-rest/bobbyhadz-js/node_modules/express/lib/router/route.js:144:13) at Route.dispatch (/home/borislav/Desktop/bobbyhadz-rest/bobbyhadz-js/node_modules/express/lib/router/route.js:114:3) at Layer.handle [as handle_request] (/home/borislav/Desktop/bobbyhadz-rest/bobbyhadz-js/node_modules/express/lib/router/layer.js:95:5) at /home/borislav/Desktop/bobbyhadz-rest/bobbyhadz-js/node_modules/express/lib/router/index.js:284:15 at Function.process_params (/home/borislav/Desktop/bobbyhadz-rest/bobbyhadz-js/node_modules/express/lib/router/index.js:346:12) at next (/home/borislav/Desktop/bobbyhadz-rest/bobbyhadz-js/node_modules/express/lib/router/index.js:280:10) at expressInit (/home/borislav/Desktop/bobbyhadz-rest/bobbyhadz-js/node_modules/express/lib/middleware/init.js:40:5)

Here is an example of how the error occurs.

index.js
const express = require('express'); const app = express(); const port = 5000; app.get('/', (req, res) => { // โ›”๏ธ ERROR: res.sendFile('index.html'); }); app.listen(port, () => { console.log(`Example app listening on port ${port}`); });

Notice that we passed a relative path to the index.html method.

# Using the __dirname variable to solve the error

One way to solve the error is to use the __dirname global variable.

index.js
// Using require() syntax const express = require('express'); const app = express(); const port = 5000; app.get('/', (req, res) => { // ๐Ÿ‘‡๏ธ using __dirname res.sendFile(__dirname + '/index.html'); }); // ๐Ÿ‘‡๏ธ /home/borislav/Desktop/bobbyhadz-js console.log(__dirname); app.listen(port, () => { console.log(`Example app listening on port ${port}`); });
The code for this article is available on GitHub

The __dirname global variable resolves to the directory that contains the current file.

The example above assumes that the index.html file is located in the root directory of your project.

Your index.html file could be as simple as the following.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <h2>bobbyhadz.com</h2> </body> </html>

If you use ES6 modules (import/export), you have to use the following syntax instead.

index.js
// Using the ES6 Modules syntax import express from 'express'; import path from 'path'; import {fileURLToPath} from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const app = express(); const port = 5000; app.get('/', (req, res) => { // ๐Ÿ‘‡๏ธ using __dirname res.sendFile(__dirname + '/index.html'); }); console.log(__dirname); app.listen(port, () => { console.log(`Example app listening on port ${port}`); });
The code for this article is available on GitHub

The __dirname variable is not available when using ES modules, so we had to construct it.

# Using the root property to solve the error

You can also use the root property of the options object to solve the error.

The res.sendFile() method takes an optional second argument - an options object.

You can set a root property on the object to specify the root directory for relative filenames.

index.js
// using require() syntax const express = require('express'); const app = express(); const port = 5000; app.get('/', (req, res) => { // ๐Ÿ‘‡๏ธ using root property res.sendFile('index.html', {root: __dirname}); }); app.listen(port, () => { console.log(`Example app listening on port ${port}`); });
The code for this article is available on GitHub

We set the root directory for relative filenames to the current directory and specified index.html as the filename.

If you still get an error, you can try setting the root property to . (the current directory).

index.js
res.sendFile('index.html', {root: '.'});

In other words, the example assumes that there is an index.html file located in the same directory as your script.

If you use ES6 Modules, use the following syntax instead.

index.js
// using ES6 Modules syntax import express from 'express'; import path from 'path'; import {fileURLToPath} from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const app = express(); const port = 5000; app.get('/', (req, res) => { // ๐Ÿ‘‡๏ธ using root property res.sendFile('index.html', {root: __dirname}); }); app.listen(port, () => { console.log(`Example app listening on port ${port}`); });

We set the root to the current directory, so Express.js looks for an index.js file in the current folder.

# Using the path.join() method to solve the error

Alternatively, you can use the path.join() method.

index.js
// using require() syntax const express = require('express'); const path = require('path'); const app = express(); const port = 5000; console.log(path.join(__dirname, 'index.html')); app.get('/', (req, res) => { // ๐Ÿ‘‡๏ธ using path.join() res.sendFile(path.join(__dirname, 'index.html')); }); app.listen(port, () => { console.log(`Example app listening on port ${port}`); });
The code for this article is available on GitHub

The path.join() method takes 2 or more paths, joins all arguments together and normalizes the resulting path.

Here is what the path resolves to in my case.

index.js
// ๐Ÿ‘‡๏ธ /home/borislav/Desktop/bobbyhadz-js/index.html console.log(path.join(__dirname, 'index.html'));

If you use ES6 Modules, use the following syntax instead.

index.js
import express from 'express'; import path from 'path'; import {fileURLToPath} from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const app = express(); const port = 5000; console.log(path.join(__dirname, 'index.html')); app.get('/', (req, res) => { // ๐Ÿ‘‡๏ธ using path.join() res.sendFile(path.join(__dirname, 'index.html')); }); app.listen(port, () => { console.log(`Example app listening on port ${port}`); });

The arguments you pass to the path.join() method may also be relative paths.

index.js
res.sendFile(path.join(__dirname, './public/index.html'));

The example assumes that your index.js file is located in a public directory that is contained in the current folder.

shell
my-project โ””โ”€โ”€ public โ””โ”€โ”€ index.html โ””โ”€โ”€ index.js

If you need to go one directory up, you would use the ../ prefix.

index.js
res.sendFile(path.join(__dirname, '../public/index.html'));

The example assumes that your public folder is located one directory up.

shell
public โ””โ”€โ”€ index.html my-project โ””โ”€โ”€ index.js

The same approach can be used if you need to go two directories up.

Simply, use a prefix of ../../

index.js
res.sendFile(path.join(__dirname, '../../public/index.html'));
The code for this article is available on GitHub

The following 2 examples are equivalent.

The first example uses path.join() to resolve the error.

index.js
res.sendFile( path.join(__dirname, '../public', 'index.html') );

The second example uses the root property with path.join().

index.js
res.sendFile( 'index.html', {root: path.join(__dirname, '../public')} );

Both examples look for an index.html file, located in a public folder that is one directory up from the current folder.

shell
public โ””โ”€โ”€ index.html my-project โ””โ”€โ”€ index.js

You can check out more examples of using path.join() in this section of the docs.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev