简体   繁体   中英

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

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM