saba
saba

Reputation: 43

Relative path of a file

I need simple help which I am not able to resolve regarding the relative path of a file. I am making this project in node and express. My file structure is:

src(folder):

     database.json
     routes(folder):

        api(folder):
             addbook.ts

Now I want to access my database file in addbook.ts using readFilesync, for that I need the relative path of the file.

Upvotes: 0

Views: 55

Answers (1)

Yury Tarabanko
Yury Tarabanko

Reputation: 45121

You can use __dirname variable.

const databaseContents = fs.readFileSync(
  path.join(__dirname, '../../database.json')
)

Upvotes: 1

Related Questions