Skip to content
Advertisement

Error: ENOENT: no such file or directory, open When trying to access a directory with fs

I am not knowledgeable in nodejs or express, I have an API running on http://localhost:3000 and one of the endpoints call a function that uses file system to read file synchronously. When I make a post request on postman, it says in the console that it can’t read a path that doesn’t exist (which does to my understanding).

Relevant code:

index.js

JavaScript

stateLogic.js (this occurs on first initialization of trieRoot)

JavaScript

readSync function used:

JavaScript

srcRoot value used:

JavaScript

Console Error:

JavaScript

Paths: stateLogic.js path from index.js, and root.txt path from stateLogic.js where it is called

Questions:

Why does it say the path doesn’t exist when it does? And what is it I am doing wrong? Thank you for your time.

Advertisement

Answer

You’ve to use absolute path instead of relative path

Modify your index.js with the below code:

JavaScript

Note: If you’re still facing the same error, then check body.path value.

Advertisement