Go back in directory in node.js (fs.readFileSync)

Viewed 994

I'm renewing my discord.js bot folder hirarchy. To make in more structured I made a folder "src" with all js-files. Now if I want to readFileSync a json file outside the src-folder, I can't figure out how to do it. Lets imagine this Folder:

Folder {
   src {
      commands {
        ping.js
      }
      main.js
   }
   config1.json
   database {
      config2.json
   }
}

I use this command to read a json file:

const config1 = JSON.parse(fs.readFileSync("____config1.json", "utf-8"));

const config2 = JSON.parse(fs.readFileSync("____config2.json", "utf-8"));

I hope my folder illustration was clear. Can someone help me how to access both of those files? The underscores are the missing characters

Thanks in advance!

2 Answers

Just like how you could use cd .. to navigate back a directory, you can do the same with fs.

const config1 = JSON.parse(fs.readFileSync("../config1.json", "utf-8"));
const config2 = JSON.parse(fs.readFileSync("../database/config2.json", "utf-8"));

../config1.json and ../database/config2.json

Related