'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

app.post('/write',(req,res)=>
{
    var body = req.body;
    console.log('endpoint reached');
    console.log(body);
    logic.append(body.key,body.path);
    res.send('Writting to state was successful');
});

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

async append(key,path)
    {
        var trieRoot = Buffer.from(programData.volatile.modularVariables.readSync(programData.persistent.paths.srcRoot,programData.volatile.modularVariables.encoding.utf8),'hex');

        console.log(trieRoot);

        var db = programData.persistent.states.singularity;
        var trie = new merkle_patricia_tree_1.SecureTrie(db,trieRoot);

        var data = programData.volatile.modularVariables.readSync(path,programData.volatile.modularVariables.encoding.utf8);

        var format = programData.volatile.modularVariables.getFormat(path);

        var name = programData.volatile.modularVariables.getName(path);

        var inData = JSON.stringify({name,format,data});
        

        console.log(`key: ${key} value: ${inData}`);

        await trie.put(Buffer.from(key),Buffer.from(inData));  

        var root = await trie.root; 

        programData.volatile.modularVariables.write(programData.persistent.paths.srcRoot,root.toString('hex'),programData.volatile.modularVariables.writeCB);

        var retGet = await trie.get(Buffer.from(key));

        console.log('Get returned:'+retGet.toString());
        console.log(`From Stack: ${root.toString('hex')} From File: ${this.malleableVar}`);

        return;
    };

readSync function used:

readSync: (file,encoding)=>{return fs.readFileSync(file,{encoding:encoding,flag:'r'})},

srcRoot value used:

srcRoot: './storage/root.txt'

Console Error:

(node:18808) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open './storage/root.txt'

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.



Solution 1:[1]

You've to use absolute path instead of relative path

Modify your index.js with the below code:

const path = require('path') // <-- import path module to use absolute path.

app.post('/write',(req,res)=>
{
    var body = req.body;
    const absPath = path.join(__dirname, body.path); // <-- absolute path
    console.log("Absolute Path: ", absPath);
    logic.append(body.key, absPath);
    res.send('Writting to state was successful');
});

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

Solution 2:[2]

we can use path module to provide absolute path for the directory

folder
  |-server.js
  |-cert.pem

Inside server.js file

const const path = require('path') 
const certpath = path.join(__dirname,'chain.pem'); // folder\chain.pem

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shariful Islam Mubin
Solution 2 KARTHIKEYAN.A