Skip to content
Advertisement

node.js TypeError: path must be absolute or specify root to res.sendFile [failed to parse JSON]

[add] So my next problem is that when i try adding a new dependence (npm install –save socket.io). The JSON file is also valid. I get this error: Failed to parse json

JavaScript

So I’ve been trying to figure out why this error has been returning. All of the files (HTML,JSON,JS) are inside the same folder on my desktop. I’m using node.js and socket.io

This is my JS file:

JavaScript

This is what is getting returned:

JavaScript

Advertisement

Answer

The error is pretty clear, you need to specify an absolute (instead of relative) path and/or set root in the config object for res.sendFile(). Examples:

JavaScript

or specify a root (which is used as the base path for the first argument to res.sendFile():

JavaScript

Specifying the root path is more useful when you’re passing a user-generated file path which could potentially contain malformed/malicious parts like .. (e.g. ../../../../../../etc/passwd). Setting the root path prevents such malicious paths from being used to access files outside of that base path.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement