I am trying to read a file with fs.readFileSync
and I'm running into some issues locating that file with __dirname
. This is my directory structure:
/Users
/me
/Documents
/myapp
/services
/myapp (node app here)
/server
/templates
/docx
-word2016_tpl.docx
/methods
/documents
-fetchDocument.ts
I am trying to read the file word2016_tpl.docx
from fetchDocument.ts
. When I read the file as a static string, everything works perfectly. Like so:
const buff = fs.readFileSync(
'/Users/me/Documents/myapp/services/myapp/server/templates/docx/word2016_tpl.docx'
);
However, clearly the above is not a good practice so I am trying to dynamically generate the file path using __dirname
.
In fetchDocument.ts
, I have:
console.log('current directory: ', __dirname);
// current directory: /server/methods/documents
My question is, where is the preceeding /Users/me/Documents/myapp/services/myapp/
and how do I access it?
Edit:
I've also tried reading the file with fs.readFileSync('/server/templates/docx/word2016_tpl.docx')
and that doesn't work