Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Is it okay to fix the typeerror: path must be absolute or specify root to res.sendfile problem?
Next
Answered
Simone Jones
  • 12
Simone Jones
Asked: May 18, 20222022-05-18T20:22:13+00:00 2022-05-18T20:22:13+00:00In: javascript

Is it okay to fix the typeerror: path must be absolute or specify root to res.sendfile problem?

  • 12

. Advertisement .

..3..

. Advertisement .

..4..

For the problem “typeerror: path must be absolute or specify root to res.sendfile.” I tried to fix it, but It doesn’t work and returns the result I want. Here is my program:

var app = require('express')();
 var http = require('http').Server(app);
 
 app.get('/', function(req, res){
  res.sendFile('index.html');
 });
 
 http.listen(3000,function(){
  console.log('listening on : 3000');
 });

and

npm ERR! Unexpected string
 npm ERR! File: /Users/John/package.json
 npm ERR! Failed to parse package.json data.
 npm ERR! package.json must be actual JSON, not just JavaScript.
 npm ERR! 
 npm ERR! This is not a bug in npm.
 npm ERR! Tell the package author to fix their package.json file. JSON.parse
MacBook-Pro:~ John$ node /Users/John/Desktop/Chatapp/index.js 
 listening on : 3000
 TypeError: path must be absolute or specify root to res.sendFile
  at ServerResponse.sendFile (/Users/John/node_modules/express/lib/response.js:389:11)
  at /Users/John/Desktop/Chatapp/index.js:5:7
  at Layer.handle [as handle_request] (/Users/John/node_modules/express/lib/router/layer.js:76:5)
  at next (/Users/John/node_modules/express/lib/router/route.js:100:13)
  at Route.dispatch (/Users/John/node_modules/express/lib/router/route.js:81:3)
  at Layer.handle [as handle_request] (/Users/John/node_modules/express/lib/router/layer.js:76:5)
  at /Users/John/node_modules/express/lib/router/index.js:234:24
  at Function.proto.process_params (/Users/John/node_modules/express/lib/router/index.js:312:12)
  at /Users/John/node_modules/express/lib/router/index.js:228:12
  at Function.match_layer (/Users/John/node_modules/express/lib/router/index.js:295:3)
 TypeError: path must be absolute or specify root to res.sendFile
  at ServerResponse.sendFile (/Users/John/node_modules/express/lib/response.js:389:11)
  at /Users/John/Desktop/Chatapp/index.js:5:7
  at Layer.handle [as handle_request] (/Users/John/node_modules/express/lib/router/layer.js:76:5)
  at next (/Users/John/node_modules/express/lib/router/route.js:100:13)
  at Route.dispatch (/Users/John/node_modules/express/lib/router/route.js:81:3)
  at Layer.handle [as handle_request] (/Users/John/node_modules/express/lib/router/layer.js:76:5)
  at /Users/John/node_modules/express/lib/router/index.js:234:24
  at Function.proto.process_params (/Users/John/node_modules/express/lib/router/index.js:312:12)
  at /Users/John/node_modules/express/lib/router/index.js:228:12
  at Function.match_layer (/Users/John/node_modules/express/lib/router/index.js:295:3)

has occurred. I’ve checked the entire command line, but still can’t find the mistake.

path must be absolute or specify root to res.sendfile
  • 2 2 Answers
  • 145 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    dttutoria Expert
    2022-06-25T07:31:58+00:00Added an answer on June 25, 2022 at 7:31 am

    The cause:  Your index.html file not being in the root directory is most likely the cause.

    Additionally, if it is in the root directory, relative referencing can not work.

    Solution:

    You must set root in the config item for res.sendFile() and (or) enter an absolute path (rather than a relative one). Examples:

    // assuming index.html is in the same directory as this script res.sendFile(__dirname + '/index.html');

    Or provide a root (used as the initial argument to res.sendFile():

    res.sendFile('index.html', { root: __dirname });
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Gabriel Cros
    2022-05-25T21:04:31+00:00Added an answer on May 25, 2022 at 9:04 pm

    In.mjs files, we don’t yet have __dirname

    hence

    res.sendFile('index.html', { root: '.' })
    • 23
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.