0

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.

2
  • I'm a bit confused about your file structure. Is this how your project is set up? src/database.json src/routes/api/addbook.ts Commented Oct 14, 2021 at 14:54
  • yes ,database.json is in my src folder and routes is inside src folder Commented Oct 14, 2021 at 15:35

1 Answer 1

1

You can use __dirname variable.

const databaseContents = fs.readFileSync( path.join(__dirname, '../../database.json') ) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.